引言:今日学习面对对象编程基础
**一切实例都可以算为对象(object),比如小狗—>金毛(实例);
类(class)用来创建对象dog1 = Dog()
,或描述对象
面对对象:
练习
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| from time import sleep class Clock(object): def __init__(self,hour=0,minute=0,second=0): self.hour = hour self.minute = minute self.second = second
def run(self): self.second += 1 if self.second == 60: self.second = 0 self.minute += 1 if self.minute == 60: self.minute = 0 self.hour += 1 if self.hour == 24: self.hour = 0
def show(self): return '%02d:%02d:%02d' % \ (self.hour, self.minute, self.second) def main(): clock = Clock(23,59,59) while True: print(clock.show()) sleep(1) clock.run()
if __name__=='__main__': main()
|
23:59:59
00:00:00
00:00:01
00:00:02
00:00:03
00:00:04
00:00:05
00:00:06
00:00:07
00:00:08
疑问
对象、方法、类的概念还是有点不清晰