我想創建一個子類的子類(Barrier是一種類型的牆,這是一種障礙),並且我想要屏障有相同的初始化作爲牆的方法,但self.type ='障礙'的例外,但我不知道如何做到這一點(我對編程非常新,所以我很抱歉,如果這是非常簡單的但我一直無法找到我理解的答案)。到目前爲止,我有:在Python 2的子類中覆蓋__init__方法的一部分
class Obstacle:
def __init__(self, type):
self.type = 'obstacle'
def __str__(self):
return "obstacle"
class Wall(Obstacle):
def __init__(self, origin, end):
self.type = 'wall'
self.origin = origin
self.end = end
# etc.... (i.e. there are more things included in here which the
# that the barrier also needs to have (like coordinate vectors etc.)
class Barrier(Wall):
def __str__(self):
return "Barrier obstacle"
我如何改變它,這樣類障礙有初始化方法的相同內容的牆壁做,除非他們的「self.type =‘壁壘’」?
我剛剛嘗試過這樣做,但在嘗試創建類的實例時遇到錯誤,因爲它表示Barrier的__init__只有一個參數,但我還需要給出起點和終點? –
@JDope:我的錯誤,改正了。 –