[英]Which is the better way to inheritance?
之间有什么区别:
class Vehicle():
def __init__(self, x, y):
self.y = y
self.x = x
class Car(Vehicle):
def __init__(self, x, y):
Vehicle.__init__(self, x, y)
class Scooter(Vehicle):
def __init__(self, x, y):
Vehicle.__init__(self, x, y)
和这个:
class Vehicle():
def __init__(self, x, y):
self.y = y
self.x = x
class Car(Vehicle):
pass
class Scooter(Vehicle):
pass
因为在子类中没有def __init__
,我得到的是同一件事,我的意思是__init__
不会提供任何效果。
您都不应该这样做。 最好的方法是使用super
。
class Vehicle():
def __init__(self, x, y):
self.y = y
self.x = x
class Car(Vehicle):
def __init__(self, x, y):
super(Car, self).__init__(x, y)
# super().__init__(x, y) # for python3
查看Raymond Hettinger(Python核心贡献者)的这篇博客文章,为什么您应该使用super
要进行子级特定的初始化时,需要__init__
方法。 假设您的子类需要将另一个参数传递给构造函数,并且该参数对于该类是唯一的,在这种情况下,确实需要__init__
方法。
class Vehicle():
def __init__(self, x, y):
self.y = y
self.x = x
class Car(Vehicle):
def __init__(self, x, y, z):
Vehicle.__init__(self, x, y)
self.z = z
class Scooter(Vehicle):
def __init__(self, x, y, z):
Vehicle.__init__(self, x, y)
self.z = z
如果您未在子类中提供__init__
方法,则它们将仅使用其父类(即继承)中定义的__init__
方法。 在前一种情况下,您将为子类覆盖__init__
方法,但是您只是在调用父类的__init__
方法。 因此,如果您不这样做(如后一种情况),它将是相同的。 后一种情况会自动继承 __init__
方法。
写同一件事的其他方法是:
class Car(Vehicle): #This is the best way to do it though
def __init__(self, x, y):
super()__init__(x, y)
要么
class Car(Vehicle):
def __init__(self, x, y):
self.x = x
self.y = y
TLDR; 它们是等效的。
我认为最好用一个例子来解释。
现在采取这种情况:
>Vehicle ---> Car,Bike,Boat,Aeroplane,Train
>[All are vehicles right]
>Things they have in common would be (say for ex.) **Price** and **Color**
但是他们不会有共同点的是什么?
>**Wheels**. The total number of wheels may differ.
>
> Car-4 Bike-2 Boat-0 Aeroplane-(**Not sure**) Train-(**Many I
guess**?)
但是你说对了吗? 因此,当我只想要一个Vehicle对象时,我不需要(或者我不能说出车轮的数量),在这种情况下,我只能使用价格和颜色进行初始化
但是,当我知道车辆的特定类型后,现在说“ 汽车”,就可以用车轮数来__init__
它。 现在,这是对象特定的初始化起主要作用的地方。
以上示例的完整示例代码:
class Vehicle():
def __init__(self, x, y):
self.color = y
self.price = x
def Horn(self):
print("Pommm...Pommmmm!!")
class Car(Vehicle):
def __init__(self, x, y,wheel):
Vehicle.__init__(self, x, y)
self.wheel = "Four Wheels man: 4"
class Scooter(Vehicle):
def __init__(self, x, y,wheel):
Vehicle.__init__(self, x, y)
self.wheel = "Just Two man : 2"
VehObj = Vehicle("5000$","Black")
VehObj.Horn()
print(VehObj.color,VehObj.price)
#However note this
carObj = Car("5000$","Black",4)
print(carObj.color,carObj.price,carObj.wheel)
#Look at this
sObj = Scooter("5000$","Black",2)
print(sObj.color,sObj.price,sObj.wheel)
输出:
Pommm...Pommmmm!!
Black 5000$
Black 5000$ Four Wheels man: 4
Black 5000$ Just Two man : 2
希望这能使您心情舒畅。
如果您不想编辑超类的__init__
方法,则调用超类的init方法是可选的。
但是,如果要编辑超类方法,则需要自定义init
class Vehicle():
def __init__(self, x, y):
self.y = y
self.x = x
class Car(Vehicle):
def __init__(self, x, y, z):
Vehicle.__init__(self, x, y)
self.z = z
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.