【发布时间】:2018-01-14 21:10:26
【问题描述】:
我想创建一个子类的子类(Barrier 是一种墙,它是一种障碍物),我希望障碍物具有相同的 init 方法除了 self.type = 'barrier' 之外,我不知道该怎么做(我对编程很陌生,所以很抱歉,如果这很简单但我做不到找到我理解的答案)。到目前为止,我有:
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"
如何更改它以使类 Barrier 具有与 Walls 相同的 init 方法内容,除了它们的“self.type = 'barrier'”?
【问题讨论】: