2017-03-02 92 views
1

很抱歉,如果我不解释它是很好,但我会尽我所能:Python的继承父类变量

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

这是什么在起作用:

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) 
+0

你不能得到你想要的东西。但是你可以使用'*参数,** kwargs'几乎到达那里。 – wim

+2

'eye_color'和'length'特定于'Parent'的*实例*,而不是整个类。鉴于'x1 =父(“蓝”,2)','x2 =父(“绿”,3)“和”y =子(“男”)','y.eye_color'或'y.length'是?至少,“Child.__ init__”需要将“Parent”实例作为参数。 – chepner

+0

您可以在'Parent'类中创建'create_child'方法,该方法将返回具有父特征的'Child'类的实例。所以你可以像'y = x.create_child()'使用它' –

回答

1

你可以尝试通过一个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) 
+0

谢谢你的解决方案是非常有帮助的!我想我会采用这种方法。 – stickfigure4

1

你问什么没有意义:

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) 

在孩子,参数eye_color和长度都来自无处。

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 

OR

class Parent: 
    def __init__(self, eye_color, length): 
     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, x.length) 
+0

谢谢,这真的很有帮助,但我认为rassar解决方案对我来说是一个更好的方法,无论如何,我会尝试编码并查看最佳使用方法。 – stickfigure4

+0

公平地说,也许OP的问题没有意义,但我发现这个页面正好适合这个解决方案,因为这是C++中的继承。 – ApplePie