python-04
装饰器:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #装饰器 2 3 4 def outer(fun): 5 def wrapper(): 6 print '验证' 7 fun() 8 print '1111111' 9 return wrapper 10 11 @outer 12 def Func1(): 13 print 'func1' 14 @outer 15 def Func2(): 16 print 'func2' 17 18 Func1() 19 Func2()
装饰器1:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 def outer(fun): 2 def wrapper(arg): 3 print '验证' 4 fun(arg) 5 print '1111111' 6 return wrapper 7 8 @outer 9 def Func1(arg): 10 print 'func1',arg 11 12 Func1('yang')
类和对象:
类:
1 class Person: 2 xx = '12345' 3 4 def __init__(self,name): 5 self.name = name 6 7 p1= Person('李阳') 8 print p1.name 9 10 p2= Person('老苟') 11 print p2.name
类中的三种形式: 方法,特性,字段
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Province: 2 3 memo = "23shengfen" #静态字段,静态字段是属于类的 4 def __init__(self,name,capital,leade): 5 6 self.name = name 7 self.capital = capital #动态字段, 类不能访问动态字段 8 self.Leade = leade 9 10 #动态方法 11 def Sport_meet(self): 12 print self.name + '开运动会' 13 14 15 @staticmethod #静态方法 静态方法属于类 16 def Foo(): 17 print '每个省要带头反腐' 18 19 @property #特性 20 def Bar(self): 21 #print self.name 22 return 'one' 23 24 hb = Province('河北','石家庄','杨') 25 sd = Province('山东','济南','许') 26 #print Province.memo 27 #print hb.name 28 #print hb.memo #对象可以访问静态字段,建议不要使用 29 30 #hb.Sport_meet() 31 #sd.Sport_meet() 32 #Province.Foo() 33 print hb.Bar #特性是这种访问形式,特性取返回值
析构函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Foo: 2 def __init__(self): 3 print '12' 4 5 def __del__(self): 6 print '解释器要销毁我了,做最后一次呐喊' 7 8 def Go(self): 9 print 'Go' 10 11 def __call__(self): 12 print 'cell' 13 14 f1 =Foo() 15 f1.Go() 16 f1()
继承:
1 #继承 2 3 class Father: 4 def __init__(self): 5 self.Fname = 'ffff' 6 7 def Func(self): 8 print 'father Func' 9 10 def Bed(self): 11 print'father chouyan hejjiu tangtou' 12 13 class Son(Father): #子类继承父类 14 def __init__(self): 15 self.Sname = 'sssss' 16 17 def Bar(self): 18 print 'son.bar' 19 ''' 20 def Bed(self): 21 print'son chouyan hejjiu' 22 ''' 23 def Bed(self): #继承 24 Father.Bed(self) 25 print 'son.赌博' 26 s1 = Son() 27 s1.Bar() 28 s1.Func() 29 s1.Bed()
类分为2类:建议用新式类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Father: # 经典类 2 def __init__(self): 3 self.Fname = 'ffff' 4 print 'father.__init__' 5 6 def Func(self): 7 print 'father Func' 8 def Bed(self): 9 print'father chouyan hejjiu tangtou' 10 11 class Son(Father): 12 def __init__(self): 13 self.Sname = 'sssss' 14 print 'son.__init__' 15 #super(Son,self).__init__() 16 Father.__init__(self) 17 18 def Bar(self): 19 print 'son.bar' 20 21 22 def Bed(self): 23 Father.Bed(self) 24 print 'son.赌博' #继承不好的,自己又学会了赌博 25 s1 = Son() 26 s1.Bar() 27 s1.Func() 28 s1.Bed()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Father(object): #新式类 object 2 def __init__(self): 3 print 'father.__init__' 4 5 def Func(self): 6 print 'father Func' 7 def Bed(self): 8 print'father chouyan hejjiu tangtou' 9 10 class Son(Father): 11 def __init__(self): 12 print 'son.__init__' 13 super(Son,self).__init__() 14 15 def Bar(self): 16 print 'son.bar' 17 18 def Bed(self): 19 Father.Bed(self) 20 print 'son.赌博' #继承不好的,自己又学会了赌博 21 s1 = Son() 22 s1.Bar() 23 s1.Func() 24 s1.Bed()
posted on 2017-01-03 00:36 augustyang 阅读(187) 评论(0) 编辑 收藏 举报