-
Notifications
You must be signed in to change notification settings - Fork 1
/
python_super用法.txt
63 lines (48 loc) · 1.58 KB
/
python_super用法.txt
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
53
54
55
56
57
58
59
60
61
62
63
Python中多继承与super()用法
Python类分为两种,一种叫经典类,一种叫新式类。两种都支持多继承。
考虑一种情形,B继承于A,C继承于A和B, 但C需要调用父类的init()函数时,前者会导致父类A的init()函数被调用2次,这是不希望看到的。而且子类要显式地指定父类,不符合DRY原则。
# 经典类
class A():
def __init__(self):
print 'A'
class B(A):
def __init__(self):
A.__init__(self)
print 'B'
class C(B, A):
def __init__(self):
A.__init__(self)
B.__init__(self)
print 'C'
采用新式类,要求最顶层的父类一定要继承于object,这样就可以利用super()函数来调用父类的init()等函数,每个父类都执行且执行一次,并不会出现重复调用的情况。而且在子类的实现中,不用到处写出所有的父类名字,符合DRY原则。
# 新式类
class A(object):
def __init__(self):
print 'A'
class B(A):
def __init__(self):
super(B, self).__init__()
print 'B'
class C(B, A):
def __init__(self):
super(C, self).__init__()
print 'C'
采用super()方式时,会自动找到第一个多继承中的第一个父类,但是如果还想强制调用其他父类的init()函数或两个父类的同名函数时,就要用老办法了。
class Person(object):
name = ""
sex = ""
def __init__(self, name, sex='U'):
print 'Person'
self.name=name
self.sex=sex
class Consumer(object):
def __init__(self):
print 'Consumer'
class Student(Person, Consumer):
def __init__(self, score,name):
print Student.__bases__
super(Student, self).__init__(name, sex='F')
Consumer.__init__(self)
self.score=score
s1 = Student(90, 'abc')
print s1.name, s1.score, s1.sex