python——多重继承

简介:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#方式一
class  A():
     def  __init__( self , name):
         self .name  =  name
         print ( "a class ..." )
 
class  B():
     def  __init__( self , age):
         self .age  =  age
         print ( "b class..." )
 
class  Sub(A, B):
     def  __init__( self , name, age, phone):
         A.__init__( self , name)
         B.__init__( self , age)
         self .phone  =  phone
         print ( "sub class..." )
 
     def  get_all_info( self ):
         print ( self .name,  self .age,  self .phone)
 
if  __name__  = =  '__main__' :
 
     =  Sub( "toby" 25 110 )
     s.get_all_info()
 
 
#方式二
class  A():
     def  __init__( self , name):
         self .name  =  name
         print ( "a class ..." )
 
class  B(A):
     def  __init__( self , name, age):
         super (B,  self ).__init__(name)
         self .age  =  age
         print ( "b class..." )
 
class  Sub(B):
     def  __init__( self , name, age, phone):
         super (Sub,  self ).__init__(name, age)
         self .phone  =  phone
         print ( "sub class..." )
 
     def  get_all_info( self ):
         print ( self .name,  self .age,  self .phone)
 
if  __name__  = =  '__main__' :
 
     =  Sub( "toby" 25 110 )
     s.get_all_info()



本文转自 TtrToby 51CTO博客,原文链接:http://blog.51cto.com/freshair/2058629

相关文章
|
4月前
|
算法 Python
Python 面向对象编程:什么是多重继承,Python 中如何处理多重继承?
Python 面向对象编程:什么是多重继承,Python 中如何处理多重继承?
|
8月前
|
算法 Python
如何实现Python中的多重继承(Multiple Inheritance)以及方法解析顺序(MRO)
如何实现Python中的多重继承(Multiple Inheritance)以及方法解析顺序(MRO)
209 0
|
12月前
|
Python
Python 在子类中调用父类方法详解(单继承、多层继承、多重继承)2
Python 在子类中调用父类方法详解(单继承、多层继承、多重继承)2
89 0
|
12月前
|
测试技术 Python
Python 在子类中调用父类方法详解(单继承、多层继承、多重继承)
Python 在子类中调用父类方法详解(单继承、多层继承、多重继承)
223 0
|
Python
Python多重继承
Python多重继承自制脑图 一个类中可以有多重父类:在 Python 中是支持多重继承的,也就是我们可以为一个类同时指定多个父类 可以在类名的( )后边添加多个类,来实现多重继承 在开发中没有特殊的情况,应该尽量避免使用多重继承,因为多重继承会让我们的代码过于复杂. 如果多个父类中有同名的方法,则会现在第一个父类中寻找,然后找第二个,然后找第三个。 多重继承的特点:前边父类的方法会覆盖后边父类的方法。
86 0
Python多重继承
|
Python
Python的封装继承多态和多重继承 | Python 主题月
Python的封装继承多态和多重继承 | Python 主题月
141 0
|
算法 C++ Python
python | 关于多重继承那些事
继承是面向对象编程的一个重要的方式 ,通过继承 ,子类就可以扩展父类的功能 。和 c++ 一样 ,在 python 中一个类能继承自不止一个父类 ,这叫做 python 的多重继承(Multiple Inheritance )。多重继承的语法与单继承类似 。
139 0
python | 关于多重继承那些事
多重继承 | Python从入门到精通:高阶篇之三十二
在Python中是支持多重继承的,也就是我们可以为一个类同时指定多个父类。这是与别的大部分语言不一致的。但在开发中没有特殊的情况,应该尽量避免使用多重继承,因为多重继承会让我们的代码过于复杂。
多重继承 | Python从入门到精通:高阶篇之三十二
|
Python 程序员 算法
python 继承与多重继承
当然,如果不支持python继承,语言特性就不值得称为“类”。派生类定义的语法如下所示: <statement-1> . . . <statement-N> 名称 BaseClassName 必须定义于包含派生类定义的作用域中。