Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- композиция — это когда один объект предоставляет другому свою функциональность частично или полностью.
- import java.util.ArrayList;
- import java.util.List;
- public class MyClass {
- public static void main(String args[]) {
- WeatherObservations observation = new WeatherObservations(10.5, 760.5);
- try {
- observation.addVisibility(10, 0, true);
- observation.addVisibility(10, 0, true);
- observation.addVisibility(10, 0, true);
- } catch (Throwable e) {
- System.out.println("Limit reached...");
- }
- }
- }
- class Visibility {
- private Integer min;
- private Integer max;
- private Boolean visabilityIsAbsent;
- public Visibility(Integer min, Integer max, Boolean visabilityIsAbsent) {
- this.min = min;
- this.max = max;
- this.visabilityIsAbsent = visabilityIsAbsent;
- }
- public Integer getMin() {
- return min;
- }
- public Integer getMax() {
- return max;
- }
- public Boolean getVisabilityIsAbsent() {
- return visabilityIsAbsent;
- }
- }
- class WeatherObservations {
- private double temperature;
- private double pressure;
- private ArrayList < Visibility > visibilities;
- public WeatherObservations(double temperature, double pressure) {
- this.visibilities = new ArrayList < Visibility > ();
- this.pressure = pressure;
- this.temperature = temperature;
- }
- public void addVisibility(Integer min, Integer max, Boolean visabilityIsAbsent) throws Throwable {
- if (visibilities.size() < 3) {
- Visibility visibility = new Visibility(min, max, visabilityIsAbsent);
- visibilities.add(visibility);
- } else {
- throw new Exception("Limit reached");
- }
- }
- }
Add Comment
Please, Sign In to add comment