代码改变世界

python class 2

2017-11-09 16:52  woodzcl  阅读(276)  评论(0编辑  收藏  举报

//test.py

1 class Employee:

  2         'all employee'

  3         empCount = 0

  4         def __init__(self, name, salary):

  5                 self.name = name

  6                 self.salary = salary

  7                 Employee.empCount += 1

  8         def prt(self):

  9                 print 'self ', self

 10                 print '__class__', self.__class__

 11         def displayCount(self):

 12                 print 'Total Employee %d' % Employee.empCount

 13         def displayEmployee(self):

 14                 print 'Name: ', self.name, ', Salary: ', self.salary

 15

 16 ee = Employee('zcl', 0)

 17 print 'doc', Employee.__doc__

 18 ee.displayCount()

 19 ee.displayEmployee()

 20 ee.prt()

 21 print 'hasattr', hasattr(ee, 'empCount'), ee.empCount

 22 print 'getattr', getattr(ee, 'empCount')

 23 print 'setattr', setattr(ee, 'empCount', 2), ee.empCount

 24 print 'delattr', delattr(ee, 'empCount')

 25 print 'hasattr', hasattr(ee, 'empCount')

 26 print '__dict__', ee.__dict__

 27 #print '__name__', ee.__name__

 28 print '__module__', ee.__module__

 29 #print '__bases__', ee.__bases__

 30

 31 class Name:

 32         def __init__(self):

 33                 self.name = 'I am father'

 34         def method(self):

 35                 print 'p', self.name

 36         def vmethod(self):

 37                 print 'vp', 'I am parent method'

 38

 39 class Point(Name):

 40         def __init__(self, x=0,y=0):

 41                 self.x = x

 42                 self.y = y

 43                 Name.__init__(self)

 44         def __del__(self):

 45                 class_name = self.__class__.__name__

 46                 print class_name, 'destroied'

 47         def submethod(self):

 48                 Name.method(self)

 49         def vmethod(self):

50                 print 'vp', 'I am child method'

 51         def __repr__(self):

 52                 print '__repr__'

 53                 return 'repr finished'

 54         def __cmp__(self, x):

 55                 if 1==x :

 56                         return 1

 57                 else:

 58                         return 2

 59         def __add__(self, other):

 60                 return Point(self.x+other.x, self.y+other.y)

 61         __privateData = "You can not see me, but properly can!"

 62

 63 pt1 = Point()

 64 pt2 = pt1

 65 pt3 = pt1

 66

 67 print id(pt1), id(pt2), id(pt3)

 68 pt1.submethod()

 69 pt1.vmethod()

 70 print repr(pt1)

 71 print cmp(pt1, 1)

 72 del pt1

 73 del pt2

 74 del pt3

 75 p3 = Point(1, 2)+Point(4, 5)

 76 print 'p3.x, p3.y', p3.x, p3.y

 77 print 'privateData ', p3._Point__privateData

//result

# python test.py
doc all employee
Total Employee 1
Name: zcl , Salary: 0
self <__main__.Employee instance at 0x7fa0a01c4200>
__class__ __main__.Employee
hasattr True 1
getattr 1
setattr None 2
delattr None
hasattr True
__dict__ {'salary': 0, 'name': 'zcl'}
__module__ __main__
140327857701664 140327857701664 140327857701664
p I am father
vp I am child method
__repr__
repr finished
1
Point destroied
Point destroied
Point destroied
p3.x, p3.y 5 7
privateData You can not see me, but properly can!
Point destroied

Finally:

python 支持多继承,这样一来,它就是c++的脚本版本了吧!!哈哈,但其它高级语言都在弱化多继承,在此,我也不多做讨论了

另外,多说一句,python里保护型成员用单下划线'_'

OK,就这么吧