python面向对象编程
重写普通方法和特殊的构造函数
类继承和普通方法重写类似,但是遇到重写构造函数的时候,必须调用超类的构造函数,否则可能无法正确初始化对象:
class Bird:
def __init__(self):
self.hungry = True
def eat(self):
if self.hungry;
print("Aaaah ...")
self.hungry = False
else:
print("No thanks")
class SongBird(Brid):
def __init__(self):
self.sound = 'squ!'
def sing(self):
print(self.sound)
>>> sb = SongBrid()
>>> sb.eat()
报错
异常:SongBird没有属性hungry,因为重写了构造函数,新的构造函数没有包含任何初始化属性hungry的代码,要避免错误需要songbird的构造函数必须调用超类的构造函数,以确保基本的初始化得以执行。
有两种解决办法:
1、调用未关联的超类构造函数
2、使用函数super
什么是调用未关联的超类构造函数
class SongBird(Brid):
def __init__(self):
Bird.__init_(self)
self.sound = 'squ!'
def sing(self):
print(self.sound)
添加一行代码,Bird.__init_(self)
对实例调用方法时,方法的参数self将自动关联到实例(称为关联的方法),如果通过类调用方法(如Bird.__init_(self))就没有实例与其关联。在这种情况下,可以随便设置参数self。 这样的方法称作未关联的,通过将这个未关联self当作参数设置为当前实例,将使用超类的构造函数来初始化SongBird对象。
使用super函数
class SongBird(Brid):
def __init__(self):
super().__init()
self.sound = 'squ!'
def sing(self):
print(self.sound)
优点:
1、不需要修改每个被重写的方法继承的类的名称,容易维护
2、在多个超类的情况下,只需要调用一次super(条件是所有超类的构造函数也是用super函数)
3、很多旧式类的super函数会自动处理
这里super首先找到父类,然后把子类的对象转换为父类的对象
ps:
一种错误
class Base(object):
def __init__(self):
print "enter Base"
print "leave Base"
class A(Base):
def __init__(self):
print "enter A"
Base().__init__()
print "leave A"
class B(Base):
def __init__(self):
print "enter B"
Base().__init__()
print "leave B"
class C(A, B):
def __init__(self):
print "enter C"
A().__init__()
B().__init__()
print "leave C"
调用init的时候少了self参数,所以这种情况仅仅是调用了超类的init函数,Base().init()是新建一个Base实例,这个实例再调用Base的构造函数,也就是重新构造本实例本身吧。这样子写子类访问不到父类构造函数里的实例变量吧。


1万+

被折叠的 条评论
为什么被折叠?



