Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将值从父类继承到子类_Python_Python 3.x_Class - Fatal编程技术网

Python 将值从父类继承到子类

Python 将值从父类继承到子类,python,python-3.x,class,Python,Python 3.x,Class,我有一个具有默认值的父类。所有其他对象都将继承 class Parent(): def __init__(self, a, b, x, y): self.a = a self.b = b self.x = x self.y = y class Child(Parent): def __init__(self, a, b, x, y): super().__init__(self, a, b)

我有一个具有默认值的父类。所有其他对象都将继承

class Parent():
    def __init__(self, a, b, x, y):
        self.a = a
        self.b = b
        self.x = x
        self.y = y

class Child(Parent):
    def __init__(self, a, b, x, y):
        super().__init__(self, a, b)
        self.x = x
        self.y = y 
我想让父母和孩子拥有相同的
a
b
。所以我不想一直写:

v = Child(a,b,x,y)
但是:


我假设您将有多个
Parent()
实例,并且希望使用其中一个的默认值创建
Child()
实例

您可以在父对象上创建factory方法:

class Parent():
    def __init__(self, a, b, x, y):
        self.a = a
        self.b = b
        self.x = x
        self.y = y

    def create_child(self, x, y):
        return Child(self.a, self.b, x, y)

child = parent.create_child(x, y)
或者给
子类
一个工厂类方法:

class Child(Parent):
    @classmethod
    def from_parent(cls, parent, x, y):
        return cls(parent.a, parent.b, x, y)
并使用:

child = Child.from_parent(parent, x, y)
选择最适合算法生成子节点所需方式的方法

后一种方法的演示:

>>> parent = Parent('foo', 'bar', 10, 20)
>>> child = Child.from_parent(parent, 52, 1)
>>> vars(child)
{'a': 'foo', 'b': 'bar', 'x': 52, 'y': 1}

也许你想使用类变量

class Parent():
    a = 2
    b = 3
    def __init__(self, x, y):
        self.x = x
        self.y = y

class Child(Parent):
    pass

v = Child(x,y)

Parent
似乎没有属性
a
b
的默认值。我建议您使用类变量对代码进行这一微小的更改

class Parent():
    a = 'foo'
    b = 'bar'
    def __init__(self, x, y):
        self.x = x
        self.y = y

class Child(Parent):
    def __init__(self, x, y):
        self.x = x
        self.y = y

如果你不把它们传进来,你希望
a
b
是什么?python应该只是选择一个随机数、字符串或对象吗?当您进行继承时,您没有父/子关系。如果你有一个
Dog
类和一个
Poodle
子类,那么
Poodle
不是
Dog
的子类。你的
a
b
(还有
x
y
z
)属性不属于这些类,而是属于这些类的实例,那么你需要什么呢(“我希望家长和孩子有相同的a和b”)没有意义。你的
孩子。
家长。
家长。
家长。
需要4个参数,你要传入3个,而
self
不应该是这些参数中的一个。你应该使用
super()
,但是您需要再次冗余设置
x
y
。您可以删除所有
子项。\uuuuu init\uuuu
方法。或者,如果
父项()
不应具有
x
y
属性,您应该将它们从
父级中删除。\uuu init\uuu
method.classmethod不起作用。@MarkoBerger:“不起作用”不会告诉我任何事情。您是否收到异常?它是否返回了错误的结果?什么?来自父级()你有4个参数,当你调用它们时,你只传递了3个参数error@MarkoBerger:您的
Child.\uuuuu init\uuuu
方法称为
super()
传入
self
,并且只传入
x
y
,但是您的
家长
类期望
a
b
x
y
,您可以删除整个
子项。uu初始化uu
。@MarkoBerger:问题不在于类方法,而在于您自己的
子项。uu初始化。@__
定义。
class Parent():
    a = 'foo'
    b = 'bar'
    def __init__(self, x, y):
        self.x = x
        self.y = y

class Child(Parent):
    def __init__(self, x, y):
        self.x = x
        self.y = y