VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • python基础教程之day25-python之继承组合(2)

print('cdrom write') 32 33 34 class Mem(All_file): 35 def read(self): 36 print('mem read') 37 38 def write(self): 39 print('mem write') 40 41 class Mem1(All_file): 42 def read(self): 43 print('mem read') 44 45 def write(self): 46 print('mem write') 47 # 48 m1=Mem1() 49 m1.read() 50 m1.write()
复制代码

8.继承顺序

复制代码
 1 #coding:utf-8
 2 class A:
 3     def test(self):
 4         print('A')
 5     pass
 6 class B(A):
 7     # def test(self):
 8     #     print('B')
 9 
10     pass
11 class C(A):
12     def test(self):
13         print('C')
14     pass
15 
16 class D(B):
17     # def test(self):
18     #     print('D')
19     pass
20 
21 class E(C):
22     # def test(self):
23     #     print('E')
24     pass
25 
26 class F(D,E):
27     # def test(self):
28     #     print('F')
29     pass
30 f1=F()
31 f1.test()   #经典类:F->D->B->A-->E-->
32 
33 print(F.__mro__)
34 
35 #F-->D->B-->E--->C--->A新式类
复制代码

9.在子类中调用父类的方法

复制代码
 1 class Vehicle:
 2     Country='China'
 3     def __init__(self,name,speed,load,power):
 4         self.name=name
 5         self.speed=speed
 6         self.load=load
 7         self.power=power
 8     def run(self):
 9         print('开动啦')
10         print('开动啦')
11 class Subway(Vehicle):
12         def __init__(self,name,speed,load,power,line):
13            Vehicle.__init__(self,name,speed,load,power)
14            self.line=line
15 
16         def show_info(self):
17             print(self.name,self.speed,self.load,self.power,self.line)
18 
19         def run(self):
20             Vehicle.run(self)
21             print('%s %s 线,开动啦' %(self.name,self.line))
22 line13=Subway('北京地铁','10km/s',1000000000,'',13)
23 
24 line13.show_info()
25 
26 line13.run()
复制代码

10.super方法的使用

复制代码
 1 class Vehicle1:
 2     Country='China'
 3     def __init__(self,name,speed,load,power):
 4         self.name=name
 5         self.speed=speed
 6         self.load=load
 7         self.power=power
 8     def run(self):
 9         print('开动啦')
10         print('开动啦')
11 class Subway(Vehicle1):
12         def __init__(self,name,speed,load,power,line):
13            # Vehicle.__init__(self,name,speed,load,power)
14            # super().__init__(name,speed,load,power)  #super(__class__,self).__init__(name,speed,load,power)
15            super(Subway,self).__init__(name,speed,load,power)
16            self.line=line
17         def show_info(self):
18             print(self.name,self.speed,self.load,self.power,self.line)
19         def run(self):
20             # Vehicle.run(self)
21             super().run()
22             print('%s %s 线,开动啦' %(self.name,self.line))
23 line13=Subway('北京地铁','10km/s',1000000000,'',13)
24 line13.show_info()
25 line13.run()
26 
27 print(line13.__class__)
复制代码


相关教程
关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们       黑ICP备07002182号