Python学习 面向对象编程
2010-08-05 07:42
288 查看
这里自行定义了一个MyTime的类,继承于系统类object。在Python里面默认的情况的下都要继承于这个类。类里面对__init__,__str__,__add__,__iadd__函数进行了重载。其实严格上讲不能叫重载,因为Python不支持重载,确切说应该叫覆盖。里面我还企图对__init__进行再次重载,显然不允许的,放在那边做个比较。
#!/usr/bin/env python class MyTime(object) : 'MyTime - operate hours, minutes and seconds' def __init__(self, h, m, s) : 'MyTime - Constructor' self.hour = h; self.min = m; self.sec = s; #def __init__(self, seconds) : # 'MyTime - Constructor' # self.hour = seconds / 3600 # seconds = seconds % 3600 # self.min = seconds /60 # self.sec = seconds % 60 def __str__(self) : #显示函数重载 'MyTime - string representation' return '%d : %d : %d' % (self.hour, self.min, self.sec) __repr__ = __str__ def __add__(self, time) : #加法重载 'MyTime - overloading the addition operator' h = m = s = 0 t = self.sec + time.sec s = t % 60 m += t /60 t = self.min + time.min + m m = t % 60 h = t / 60 t = self.hour + time.hour + h h = t % 24 return self.__class__(h, m, s) def __iadd__(self, time) : #自增重载 'MyTime - overloading the in-place addition operator' t = self.sec + time.sec self.sec = t % 60 self.min += t / 60 t = self.min + time.min self.min = t % 60 self.hour += t /60 t = self.hour + time.hour self.hour = t % 24 return self #测试部分 t1 = MyTime(12,34,57) t2 = MyTime(8,6,56) print 't1 >> ', t1 print 't2 >> ', t2 print 't1 + t2 >> ', t1 + t2 t1 += t2 print 't1 += t2 >> ', t1 #测试结果 #t1 >> 12 : 34 : 57 #t2 >> 8 : 6 : 56 #t1 + t2 >> 20 : 41 : 53 #t1 += t2 >> 20 : 41 : 53
相关文章推荐
- Python学习札记(三十五) 面向对象编程 Object Oriented Program 6
- 【Python学习笔记】面向对象编程
- Python学习笔记——面向对象编程
- python小菜的学习之路 之 面向对象编程
- Python学习笔记--2--面向对象编程
- Python学习札记(三十七) 面向对象编程 Object Oriented Program 8 @property
- Python之面向对象编程学习
- python-学习笔记1-面向对象编程
- Python3学习笔记6-类,面向对象编程
- python 学习笔记day08-面向对象编程
- Python学习札记(三十四) 面向对象编程 Object Oriented Program 5
- python学习[2]_Python中面向对象编程1
- 我的Python学习之路之基本语法-面向对象编程
- python学习(7)——面向对象编程
- python 面向对象编程学习
- Python基础学习代码之面向对象编程
- Python学习笔记 7—面向对象编程
- 2017.6.27 python学习 面向对象编程
- 【Python学习笔记】面向对象编程:类和实例
- 『Python学习』python 核心编程——面向对象编程学习笔记