类的继承

  1 class foo():
  2     def __init__(self):
  3         pass
  4     def __del__(self):#析构函数
  5         print '有人要搞我啦!'
  6 
  7 
  8 a=foo()
  9 
 10 
 11 ---------------------------------------------------------------------
 12 
 13 #!/usr/bin/env python
 14 #coding:utf-8
 15 
 16 class foo():
 17     def __init__(self):
 18         pass
 19     def __del__(self):#这个函数永远是最后执行,在对象销毁的时候执行,对象生存的最后一次呐喊,当判断后面没用在调用这个类的时候,这个执行
 20         print '有人要搞我啦!'
 21     def go(self):
 22         print '你大爷'
 23     def __call__(self):
 24         print 'call'
 25 
 26 
 27 a=foo()
 28 a.go()
 29 a()#专门调用类的格式,调用其中的call
 30 
 31 ---
 32 decrator
 33 
 34 -------
 35 #!/usr/bin/env python
 36 #coding:utf-8
 37 
 38 
 39 class father:
 40     def __init__(self):
 41         self.name='niama'
 42 
 43     def foo(self):
 44         print '111'
 45 
 46     def bad(self):
 47         print '抽烟喝酒打牌'
 48 
 49 
 50 class mother(father):#继承父类
 51     def __init__(self):
 52         self.name='nidaye'
 53 
 54     def fo(self):
 55         print '222'
 56 
 57     #def bad(self):#此处实现了方法的重写
 58     #   print '戒了'
 59     def bad(self):
 60         father.bad(self) 
 61         print 'son.赌博'
 62 
 63 
 64 b=mother()
 65 b.foo()
 66 b.bad()
 67 #######################################################################
 68 #!/usr/bin/env python
 69 #coding:utf-8
 70 
 71 
 72 class father(object):#第二种方法需要加上object
 73     def __init__(self):
 74         self.name='niama'
 75         print 'father----'
 76 
 77     def foo(self):
 78         print '111'
 79 
 80     def bad(self):
 81         print '抽烟喝酒打牌'
 82 
 83 
 84 class mother(father):#继承父类
 85     def __init__(self):
 86         self.name='nidaye'
 87         father.__init__(self)#第一种方法调用父类的构造方法
 88         #print 'mother---'
 89         super(mother,self).__init__()#第二种方法调用父类的构造方法中的内容
 90 
 91     def fo(self):
 92         print '222'
 93 
 94     #def bad(self):#此处实现了方法的重写
 95     #   print '戒了'
 96     def bad(self):
 97         father.bad(self)
 98         print 'son.赌博'
 99 
100 
101 b=mother()
102 b.foo()
103 b.bad()
104 
105 ###############################################################
106 如果类继承了object类,则为新式类,如果没有object则为经典类
107 
108 两个类的用法
109 
110 
111 #!/usr/bin/env python
112 #coding:utf-8
113 
114 class a(object):#此时就要考虑python的新式继承和经典继承,如果这边没有object就是经典继承,此时的结果就是先查找b有没有save方法,然后再查找a,最后查找c,所以这个不是很合理,
115     #而此时如果加了object就成了新式继承,就是先从b开始找,如果b没有就从c开始找,最后再找a
116     def __init__(self):
117         print 'this is a'
118 
119     def save(self):
120         print 'save method from a'
121 
122 class b(a):
123     def __init__(self):
124         print 'this is b'
125 
126 class c(a):
127     def __init__(self):
128         print 'this is c'
129     def save(self):
130         print 'this is c______'
131 
132 class d(b,c):
133     def __init__(self):
134         print 'this is d'
135 
136 
137 p=d()
138 p.save()
139 
140 
141 
142 
143 #################################################
144 静态字段属于累,动态字段属于实例 

 

 1 #!/usr/bin/env python
 2 #coding:utf-8
 3 
 4 class person(object):
 5     m = 'nima'
 6     def __init__(self, name, gene, weight):
 7         self.name = name
 8         self.__gene = gene
 9         if name != 'bill':
10             self.Gender = ''
11         self.weight = weight
12         self.age = 19
13 
14     def talk(self):
15         print 'xxx'
16 
17     def fight(self, weight):
18         if self.weight > weight:
19             print ''
20         else:
21             print ''
22         person.fun()#属于谁就怎么调用
23     @staticmethod#静态方法也是属于类的
24     def fun():
25         print 'fuck'
26 p1 = person('bill', 'man', 170)
27 p1.fight(160)
28 p2 = person('n2', 'woman', 180)
29 p2.fight(p1.weight)
30 
31 print p2.__dict__#获取的只是字段
32 print dir(p2)#这个包含方法,字段,还有一些字段的功能
33 print vars(p2)
34 print person.m#静态字段属于累,动态字段属于实例 ,最好通过这种方式去获取类的静态字段,不要去通过对象.m访问,其他的语言是访问不了的
35 #对于静态字段而言,是保存在类中的,在内存空间中只保存一份。通过对象访问抽象的感觉越权了。
36 person.fun()###

 

posted @ 2017-05-29 09:37  腐汝  阅读(148)  评论(0编辑  收藏  举报