简体   繁体   English

Python从父类继承变量

[英]Python inherit variables from parent class

Sorry if I don't explain it that well but I'll try my best: 对不起,如果我不解释那么好,但我会尽我所能:

So I want to inherit the variables from the Parent class, but I don't want to pass them again when creating an instance of the Child class because I think that is redundant. 所以我想从Parent类继承变量,但我不想在创建Child类的实例时再次传递它们,因为我认为这是多余的。 I just want to use the eye color from the Parent for example. 我只是想使用来自Parent的眼睛颜色。 See the example code below for what I mean 请参阅下面的示例代码,了解我的意思

This is what works: 这是有效的:

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)

This is what I somehow want: 这就是我想要的:

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)

What you ask does not make sense: 你问的是没有意义的:

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)

In child, the parameters eye_color and length come from nowhere. 在儿童中,参数eye_color和长度来自任何地方。

rassar example is good if you want to reuse a parent object. 如果要重用父对象, rassar示例很好。

You can also do the following: 您还可以执行以下操作:

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

OR 要么

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)

You could try passing a Parent instance to the Child initializer...That's probably the closest you'll get. 您可以尝试将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)

Another thing you could do is hold a last_parent variable: 你可以做的另一件事是持有一个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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM