精華區beta b865060xx 關於我們 聯絡資訊
呵呵!為應老師要求,這個Clock物件必須寫得相當的『複雜~~~』 為什麼呢?因為我們必須把『秒』、『時』、『分』看成是不同的 物件,恩!這要怎麼寫呢,昨天研究了二十分鐘寫了如下程式, 大家看了別吐血^_^ class Clock implements Runnable { class Hour { private int nHour; public void Tick() { nHour ++; if (nHour > 12) { nHour = 0; } } public String GetText() { return "" + nHour; } } class Minute { private Hour fHour; private int nMinute; Minute(Hour aHour) { fHour = aHour; } public void Tick() { nMinute ++; if (nMinute == 60) { nMinute = 0; fHour.Tick(); } } public String GetText() { if (nMinute > 9) return "" + nMinute; else return "0" + nMinute; } } class Second { private Minute fMinute; private int nSecond; Second(Minute aMinute) { fMinute = aMinute; } public void Tick() { nSecond ++; if (nSecond == 60) { nSecond = 0; fMinute.Tick(); } } public String GetText() { if (nSecond > 9) return "" + nSecond; else return "0" + nSecond; } } private Hour fHour; private Minute fMinute; private Second fSecond; private Label fLabel; private Thread Spirit; Clock(Label aLabel) { fLabel = aLabel; fHour = new Hour(); fMinute = new Minute(fHour); fSecond = new Second(fMinute); Spirit = new Thread(this); Spirit.start(); } public void Tick() { fSecond.Tick(); } public void run() { while(true) { try { Thread.sleep(1000); } catch(InterruptedException q) { return; } Tick(); UpdateClock(); } } public String GetText() { return fHour.GetText() + ":" + fMinute.GetText() + ":" + fSecond.GetText(); } public void UpdateClock() { fLabel.setText(GetText()); } } (註:這只是一個inner class不是完整的程式喔!可不能run的:)) -- Power is unreal. Money is useless. Doctrine is a mistake in human society. The human society, however, is a mistake itself. Change brings unstability. Evolution leads to war. Life has no meaning if you want some. Goal for living must be obtained by yourself. Nothing counts if you don't seek for it. -- ※ 發信站: 批踢踢實業坊(ptt.m8.ntu.edu.tw) ◆ From: h96.s18.ts30.hi