首页 > 代码库 > Java程序day1020

Java程序day1020

时钟程序

 1 package com.lovo; 2  3 import java.util.Calendar; 4  5 /** 6  * 时钟类 7  */ 8 public class Clock { 9     private int hour;            //10     private int minute;            //11     private int second;            //12     13     /**14      * 构造器15      */16     public Clock() {17         Calendar cal = Calendar.getInstance();18         hour = cal.get(11);19         minute = cal.get(12);20         second = cal.get(13);21     }22     23     /**24      * 走字25      */26     public void go() {27         second++;28         if(second == 60) {29             second = 0;30             minute++;31             if(minute == 60) {32                 minute = 0;33                 hour++;34                 if(hour == 24) {35                     hour = 0;36                 }37             }38         }39     }40     41     /**42      * 显示时间43      * @return 当前时间的字符串44      */45     public String showTime() {46         String time = "";47         if(hour < 10) {48             time += "0";49         }50         time += hour + ":";51         if(minute < 10) {52             time += "0";53         }54         time += minute + ":";55         if(second < 10) {56             time += "0";57         }58         time += second;59         return time;60     }61 }

验证程序

 1 package com.lovo; 2  3 public class Test04 { 4  5     public static void main(String[] args) throws Exception { 6         Clock c = new Clock(); 7         while(true) { 8             System.out.println(c.showTime()); 9             c.go();10             Thread.sleep(1000);    // 让当前程序休眠1秒钟11         }12     }13 }

总结:
对象是表示现实世界的所有东西,一个对象就代表一个具体的东西。对象主要由属性和行为组成。属性是表示对象由什么组成,行为则表示对象可以做什么。类是对象的“蓝图”或“模版”。

 

Java程序day1020