本文初衷在于记录笔者在Java编程的学习历程中的一些足迹,同时希望在CSDN中能够与其他初学者们有互相学习交流的机会 (๑`・ᴗ・´๑)
一、运行结果
调皮一下 (づ◡ど) 加了个"Happy"
代码如下:
package ClockTwstOne;
class test{ //friendly的类可以有多个,命名随意
void happy(){ //但这种类只可以在同一个包中使用;
System.out.println("Happy");
}
}
public class DisPlay { //pubilc 类再一个文件中只能有一个且与文件名一致;
private int value = 0;
private int limit = 0; //普通成员变量
private static int step; //类变量,广属于类而不专属于任何一个对象
public DisPlay(int limit){
this.limit = limit;
}
public void increase(){
value++;
if(value == limit){
value = 0;
}
}
public int getValue(){
return value;
}
public static void TestStatic(){ //类函数
System.out.println("运行了类函数");
}
public static void main(String args[]){
System.out.println("我运行了DisPlay的Main函数!");
DisPlay d1 = new DisPlay(10);
DisPlay d2 = new DisPlay(10);
System.out.println(d1.step);
System.out.println(d2.step);
TestStatic(); //在同为类函数的 main 函数中调用
d2.step = 3; //类函数 TestStatic()
d1.value = 3;
step = 9;
System.out.println(step);
System.out.println(d1.step);
System.out.println(d2.step);
}
void happy(){
System.out.println("happy!!!");
}
public void publicHappy(){
System.out.println("public Happy!!!");
}
}
2.编写类ClockTestOne
代码如下:
package ClockTwstOne;
import java.util.Timer;
import java.util.TimerTask;
public class ClockTestOne {
private DisPlay hour = new DisPlay(24); //1、用类 DisPlay 的对象来
private DisPlay minute = new DisPlay(60);// 构成类 ClockTestOne 的成员变量;
private DisPlay second = new DisPlay(60);//2、三个 DisPlay 的对象通过 ClockTestOne
Timer time = new Timer(); // 来联系彼此,保证了彼此的独立性;
public void start(){
// while (true){
time.schedule(new TimerTask() {
@Override
public void run() {
second.increase();
if(second.getValue() == 0) {
minute.increase();
if (minute.getValue() == 0) {
hour.increase();
}
}
System.out.printf("%02d:%02d:%02dn",
hour.getValue(),minute.getValue(),second.getValue());
}
},1000,1000); //延迟 1秒 执行,并不停每隔 1秒 执行一次
}
public static void main(String[] args){
test t = new test(); //乱入的
t.happy(); //"Happy" ( > v < )
ClockTestOne clock = new ClockTestOne();
clock.start();
}
}
总结
总的来讲就是这些啦,一些小点都注释在代码中了,祝阅者&笔者开心每一天 (๑`・ᴗ・´๑)!
tips:笔者是将两个类放在了两个 .Java 文件里的,放在同一个里…嗯… … 太冗长。



