如何用对象来模拟一个时钟,11:09;
用一个对象还是两个对象;;;;一个用两个对象那怎么对其进行交互联系(hour 怎么和 mintue 进行联系);
方法是用另外一个colock对其进行交互;
设立两个类 ①class Display ②class Colock
mintue 和 colock
</pre><span style="font-size:24px;"> 都是Display 分别表示小时和分钟------colock则对他们进行交互</span><p></p><p><span style="font-size:18px;">代码如下:</span></p><p><span style="font-size:18px;"></span></p><pre name="code" class="java">package org.clock;
public class Display {
private int limit = 0;
private int value = 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());
// }
// }
}
2.在colock中添加两个属性 分别为hour 和 mintue对象
package org.clock;
public class Clock1 {
private Display hour = new Display(24);
private Display minute = new Display(60);
<span style="font-size:18px;"> //在colock中添加两个属性 分别为hour 和 mintue对象</span>
public void start() {
for ( ; ;) {
minute.increase();
if(minute.getValue() == 0) {
hour.increase();
}
System.out.printf("%02d:%02d\n", hour.getValue(), minute.getValue());
}
}
public static void main(String[] args) {
Clock1 c = new Clock1();
c.start();
}
}
创建类colock的对象,而其中有包含两个对象hour,mintue; 对象中又包含了对象,这样才可以对其进行交互
-----------------------------------------------------------------------------------------------------------------------------------------------------
2.代码中,表示分钟的对象和表示小时的对象没有直接交互。如果想要做直接交互,让表示分钟的对象在翻转的时候直接调用表示小时的对象的那个increase函数,代码需要怎样修改?
:对其进行重载,分别表示hour.increase()和mintue.increase();;;;;;;;
hour.increase()只表示values mintue中包含了hour.increase()
调用时只要进行minute调用,满足条件在mintue中同样再去调用hour
<span style="font-size:18px;">package org.clock;
public class Display {
private int limit = 0;
private int value = 0;
public Display(int limit) {
this.limit = limit;
}
public void increase() {
value++;
if(value == limit)
value = 0;
}
public void increase(Display d) {
value++;
if(value == limit) {
value = 0;
d.increase();
//调<span style="font-size:14px;">用时只要进行minute调用,满足条件在mintue中同样再去调用hour</span>
}
}
public int getValue() {
return value;
}
// public static void main(String[] args) {
// Display d = new Display(24);
// for ( ; ; ) {
// d.increase();
// System.out.println(d.getValue());
// }
// }
}
</span>
package org.clock;
public class Clock1 {
private Display hour = new Display(24);
private Display minute = new Display(60);
public void start() {
for ( int i = 1; i <= 100; i++) {
minute.increase(hour);
System.out.printf("%02d:%02d\n", hour.getValue(), minute.getValue());
}
}
public static void main(String[] args) {
Clock1 c = new Clock1();
c.start();
}
}
-------------------------------------------------------------------------------------------------------------------
总结:但是用第二种方法不仅多写一份代码,而且耦合性太高,主要是懂得一种思想吧!写代码多写,多想!!
致此!
-------------------------------------------------------------------------------------------------------------------
public class Display {
private int limit = 0;
private int value = 0;
public Display() {
}
public Display(int limit) {
this.limit = limit;
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public void increase() {
value++;
if (value == limit)
value = 0;
}
}
------------------------------------------------------------------------------------------------------------------
public class Clock {
private Display hour = new Display(24);
private Display minute = new Display(60);
private Display second = new Display(60);
public Clock() {
}
public void tick() {
second.increase();
if (second.getValue() == 0) {
minute.increase();
if (minute.getValue() == 0)
hour.increase();
}
}
public void print() {
System.out.printf("%02d:%02d:%02d\n", hour.getValue(), minute.getValue(), second.getValue()) ;
}
}
public class Main {
public static void main(String[] args) {
Clock clock = new Clock();
for ( ; ; ) {
clock.tick();
clock.print();
}
}
}