在Python2的子类中重写_init__方法的一部分

在Python2的子类中重写_init__方法的一部分,python,class,subclass,Python,Class,Subclass,我想创建一个子类的子类(Barrier是墙的一种类型,它是障碍物的一种),我想让Barrier具有与wall相同的init方法,除了self.type='Barrier',但我不确定如何做到这一点(我对编程非常陌生,如果这很简单,我很抱歉,但我还没有找到我理解的答案)。到目前为止,我有: class Obstacle: def __init__(self, type): self.type = 'obstacle' def __str__(self):

我想创建一个子类的子类(Barrier是墙的一种类型,它是障碍物的一种),我想让Barrier具有与wall相同的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'”?

调用
Wall
版本后,只需覆盖该属性:

class Barrier(Wall):
    def __init__(self, origin, end):
        super().__init__(origin, end)
        self.type = 'barrier'

    def __str__(self):
        return "Barrier obstacle"

您可能想考虑使用类属性,但是,您的实例属性没有一个对类的每个实例都是动态的和特定的。这些类的<代码>类型< /C>属性肯定不会从一个实例变成另一个实例:

class Obstacle:
    type = 'obstacle'

    def __str__(self):
        return self.type

class Wall(Obstacle):
    type = 'wall'

    def __init__(self, origin, end):
        super().__init__()
        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):
    type = 'barrier'

调用
Wall
版本后,只需覆盖该属性:

class Barrier(Wall):
    def __init__(self, origin, end):
        super().__init__(origin, end)
        self.type = 'barrier'

    def __str__(self):
        return "Barrier obstacle"

您可能想考虑使用类属性,但是,您的实例属性没有一个对类的每个实例都是动态的和特定的。这些类的<代码>类型< /C>属性肯定不会从一个实例变成另一个实例:

class Obstacle:
    type = 'obstacle'

    def __str__(self):
        return self.type

class Wall(Obstacle):
    type = 'wall'

    def __init__(self, origin, end):
        super().__init__()
        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):
    type = 'barrier'
由于“type”似乎取决于类,因此我不会将
type
属性放在对象中,而是放在类级别上:

class Obstacle:

    type = 'obstacle'

    def __init__(self):
        # do something
        pass

    def __str__(self):
        return "obstacle"

class Wall(Obstacle):

    type = 'wall'

    def __init__(self, origin, end):
        super().__init__()
        self.origin = origin
        self.end = end

class Barrier(Wall):

    type = 'barrier'

    def __str__(self):
        return "Barrier obstacle"
因此,我们仍然可以灵活地将特定类型添加到特定对象中。但默认情况下,如果我们查询某个类型,它将执行回退并返回在类级别定义的类型。

,因为“类型”似乎取决于类,我不会将
type
属性放在对象中,而是放在类级别上:

class Obstacle:

    type = 'obstacle'

    def __init__(self):
        # do something
        pass

    def __str__(self):
        return "obstacle"

class Wall(Obstacle):

    type = 'wall'

    def __init__(self, origin, end):
        super().__init__()
        self.origin = origin
        self.end = end

class Barrier(Wall):

    type = 'barrier'

    def __str__(self):
        return "Barrier obstacle"

因此,我们仍然可以灵活地将特定类型添加到特定对象中。但是默认情况下,如果我们查询一个
类型
,它将执行回退并返回在类级别上定义的
类型。

我刚刚尝试过这样做,但在尝试创建类实例时出错,因为它说Barrier的init只有一个参数,但我还需要给出起点和终点?我刚刚尝试过这样做,但当我尝试创建类的实例时,我会出错,因为它说Barrier的init只有一个参数,但我还需要给出起点和终点?以防万一OP不理解:这里的区别在类dentition下使用
self.type='barrier'
type='barrier'
之间,后者创建了一个“静态”变量。这基本上意味着
type
属性将在
Barrier
的所有实例中共享。这里的好处是,由于
type='Barrier'
不会在每个实例中更改,因此您可以让所有实例共享一个属性,而不是让每个实例都有自己的属性。以防万一OP会这样做esn不明白:在
中使用
self.type='barrier'
与在类dentition下使用
type='barrier'
之间的区别在于后者创建了一个“静态”变量。这基本上意味着
type
属性将在
Barrier
的所有实例中共享。这里的好处是,由于
type='Barrier'
不会在每个实例中更改,因此可以让所有实例共享一个属性,而不是让每个实例都有自己的属性。