对象的识别和对象的交互

对象的识别

public class Display {
    private int value = 0;
    private int limit = 0;

    public Display(int limit){
        this.limit=limit;
    }
    public void increase(){
        value++;
        if (value==limit){
            value=0;
        }
    }
    public int getValue(){
        return value;
    }

    public static void main(String[] args) {
        Display d = new Display(24);
        for (;;){
            d.increase();
            System.out.println(d.getValue());
        }
    }
}

让时钟的到达24变成0

 

 

对象的交互

用一个类的两个对象构造了另外一个类,在另外一个类里面有两个别的类的对象

public class Clock {
    private Display hour = new Display(24);
    private Display minute = new Display(60);

    private void start(){
        while (true){
            minute.increase();
            if (minute.getValue()==0){
                hour.increase();
            }
            System.out.printf("%02d:%02d\n" , hour.getValue(), minute.getValue());
        }
    }

    public static void main(String[] args) {
        Clock clock = new Clock();
        clock.start();
    }
}

 

 

posted @ 2022-06-10 16:01  魔光领域  阅读(25)  评论(0)    收藏  举报