簡體   English   中英

哪種更好的繼承方式?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM