2017-06-30 108 views
1

是否有此之間的任何差別:與哪種方法是更好的繼承方式?

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 

因爲沒有子類中,我得到了同樣的事情,我的意思是__init__不提供任何效果。

+0

第二個不是絕對的繼承,因爲兩個類都不能從外部訪問。現在你已經改變了代碼 – Rahul

+0

我認爲沒有什麼區別,因爲在第一個例子中,你基本上繞過了自己的類'init' –

+0

我更喜歡第一個版本,因爲你可以_expand_ __init__'方法而不覆蓋繼承代碼 – Aemyl

回答

0

,當你想要做孩子的具體初始化你需要__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 
-1

調用父類的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 
0

如果不提供在子類的__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;它們是等價的。

1

你should't做他們的任何。最好的方法是使用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 

檢查this博客文章由雷蒙德赫廷傑(Python核心貢獻者)爲什麼你應該使用super

0

我認爲這將用一個例子來最好的解釋。

現在採取這樣的場景:

>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**?) 

但你明白了嗎?所以,當我想只是有一個車輛對象我不希望(或我不能告訴的數)在這種情況下,我只能用剛剛價格和顏色初始化

然而當我知道具體類型車輛現在我可以__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 

。希望清除你。