对象的识别和对象的交互
作者:互联网
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
无限循环
对象的交互
用一个类的两个对象构造了另外一个类,在另外一个类里面有两个别的类的对象
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(); }
标签:对象,private,public,getValue,limit,value,识别,交互,Display 来源: https://www.cnblogs.com/yuzong/p/16398115.html