Skip to content

Instantly share code, notes, and snippets.

@starlightme
Last active August 29, 2015 14:09
Show Gist options
  • Save starlightme/78271b44deae2f605ff7 to your computer and use it in GitHub Desktop.
Save starlightme/78271b44deae2f605ff7 to your computer and use it in GitHub Desktop.
Java时钟类
package demo;
class Clock{
int hour,minute,second;
//构造函数
public Clock(){
this.hour=0;
this.minute=0;
this.second=0;
}
//设定时间
public void setup(int hour,int minute,int second){
this.hour=hour;
this.minute=minute;
this.second=second;
}
//输出时间
public void show(){
System.out.println(hour+":"+minute+":"+second);
}
//使时钟前进1秒
public void incSecond(){
this.second++;
}
}
public class Clcok {
public static void main(String[] args) {
Clock a=new Clock();
a.setup(2, 12, 13);
a.incSecond();
a.show();
}
}
@starlightme
Copy link
Author

一个简单的类的书写,让自己熟悉下OOP的一些基础操作:类的描述和对象的实例化,析构函数的书写,如何传参和处理与类中本身变量的关系...

因为只看了OOP相关的两章节,所以没有加入输入函数和java中的时间函数进行优化。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment