Python从父类继承变量

2024-06-12 17:59:10 发布

您现在位置:Python中文网/ 问答频道 /正文

抱歉,如果我解释不清楚,我会尽力的:

所以我想从父类继承变量,但是我不想在创建子类实例时再次传递它们,因为我认为这是多余的。例如,我只想用父母的眼睛颜色。参见下面的示例代码了解我的意思

这就是工作原理:

class Parent:
    def __init__(self, eye_color, length):
        self.eye_color = str(eye_color)
        self.length = length


class Child(Parent):
    def __init__(self, gender, eye_color, length):
        super().__init__(eye_color, length)
        self.gender = str(gender)

x = Parent("Blue", 2)
y = Child("Men", "Blue", 2)

print(x.eye_color, x.length)
print(y.gender, x.length)

这就是我想要的:

class Parent:
    def __init__(self, eye_color, length):
        self.eye_color = str(eye_color)
        self.length = length


class Child(Parent):
    def __init__(self, gender):
        super().__init__(eye_color, length)
        self.gender = str(gender)

x = Parent("Blue", 2)
y = Child("Men")

print(x.length, x.eye_color)
print(y.gender, x.length)

Tags: selfchildinitdefbluegenderlengthclass
2条回答

你的要求毫无意义:

class Parent:
    def __init__(self, eye_color, length):
        self.eye_color = str(eye_color)
        self.length = length


class Child(Parent):
    def __init__(self, gender):
        super().__init__(eye_color, length)
        self.gender = str(gender)

x = Parent("Blue", 2)
y = Child("Men")

print(x.length, x.eye_color)
print(y.gender, x.length)

在child中,参数eye_color和length来自任何地方。

如果要重用父对象,rassar示例很好。

您还可以执行以下操作:

class Parent:
    # def __init__(self, eye_color=(default value here), length=(default value here)):
    def __init__(self, eye_color="", length=0):
        self.eye_color = str(eye_color)
        self.length = length

或者

class Parent:
    def __init__(self, eye_color="", length=0):
        self.eye_color = str(eye_color)
        self.length = length

class Child(Parent):
    def __init__(self, gender, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.gender = str(gender)

x = Parent("Blue", 2)
y = Child("Men") # Work with first example of Parent
y = Child("Men", eye_color="Blue", length=2) # Work with first
y = Child("Men", "Blue", 2) # Work with second example

print(x.length, x.eye_color)
print(y.gender, y.length)

您可以尝试将一个Parent实例传递给Child初始值设定项…这可能是最接近的。

class Parent:
    def __init__(self, eye_color, length):
        self.eye_color = str(eye_color)
        self.length = length


class Child(Parent):
    def __init__(self, gender, parent):
        super().__init__(parent.eye_color, parent.length)
        self.gender = str(gender)

x = Parent("Blue", 2)
y = Child("Men", x)

print(x.length, x.eye_color)
print(y.gender, x.length)

另一个方法是保存一个last_parent变量:

global last_parent

class Parent:
        def __init__(self, eye_color, length):
            self.eye_color = str(eye_color)
            self.length = length
            last_parent = self


class Child(Parent):
    def __init__(self, gender):
        super().__init__(last_parent.eye_color, last_parent.length)
        self.gender = str(gender)

x = Parent("Blue", 2)
y = Child("Men")

print(x.length, x.eye_color)
print(y.gender, x.length)

相关问题 更多 >