Clock program
1 PackageCom.lovo;2 3 ImportJava.util.Calendar;4 5 /**6 * Clock class7 */8 Public classClock {9 Private intHour//whenTen Private intMinute//points One Private intSecond//seconds A - /** - * Constructor the */ - PublicClock () { -Calendar cal =calendar.getinstance (); -hour = Cal.get (11); +minute = Cal.get (12); -Second = Cal.get (13); + } A at /** - * Walk the word - */ - Public voidGo () { -second++; - if(Second = = 60) { inSecond = 0; -minute++; to if(Minute = = 60) { +minute = 0; -hour++; the if(Hour = = 24) { *Hour = 0; $ }Panax Notoginseng } - } the } + A /** the * Show Time + * @returnstring of the current time - */ $ PublicString ShowTime () { $String time = ""; - if(Hour < 10) { -Time + = "0"; the } -Time + = Hour + ":";Wuyi if(Minute < 10) { theTime + = "0"; - } WuTime + = minute + ":"; - if(Second < 10) { AboutTime + = "0"; $ } -Time + =second; - returnTime ; - } A}
Validation Program
1 PackageCom.lovo;2 3 Public classTest04 {4 5 Public Static voidMain (string[] args)throwsException {6Clock C =NewClock ();7 while(true) {8 System.out.println (C.showtime ());9 c.go ();TenThread.Sleep (1000);//let the current program sleep for 1 seconds One } A } -}
Summarize:
Objects are all things that represent the real world, and an object represents a specific thing. Objects are primarily composed of properties and behaviors. A property is a representation of what the object is made of, and the behavior represents what the object can do. A class is the "blueprint" or "template" of an object.
Java Program day1020