如何在子 class 中调用父 class?
How do I call on a parent class in a subclass?
我需要创建一个对 Plant 的 UNBOUND 方法调用以设置名称和叶子,但我不知道如何操作。感谢任何帮助。
我的代码:
class Plant(object):
def __init__(self, name : str, leaves : int):
self.plant_name = name
self.leaves = leaves
def __str__(self):
return "{} {}".format(self.plant_name, self.leaves)
def __eq__(self, plant1):
if self.leaves == plant1.leaves:
return self.leaves
def __It__(self, plant1):
if self.leaves < plant1.leaves:
print ("{} has more leaves than {}".format(plant1.plant_name, self.plant_name))
return self.leaves < plant1.leaves
elif self.leaves > plant1.leaves:
print ("{} has more leaves than {}".format(self.plant_name, plant1.plant_name))
return self.leaves < plant1.leaves
class Flower(Plant):
def __init__(self, color : str, petals : int):
self.color = color
self.petals = petals
def pick_petal(self.petals)
self.petals += 1
作业的确切措辞:
Create a new class called Flower. Flower is subclassed from the Plant class; so besides name, and leaves, it adds 2 new attributes; color, petals. Color is a string that contains the color of the flower, and petal is an int that has the number of petals on the flower. You should be able to create an init method to setup the instance. With the init you should make an UNBOUND method call to plant to setup the name and leaves. In addition, create a method called pick_petal that decrements the number of petals on the flower.
"unbound method call" 意味着您在 class 上调用方法,而不是在 class 的实例上调用方法。这意味着类似于 Plant.some_method
。
在此上下文中唯一有意义的未绑定调用是调用基 class 的 __init__
方法。好像可以满足"setup the names and leaves"的要求,以前也是比较常见的继承方式
看起来像这样:
class Flower(Plant):
def __init__(self, name, leaves, color, petals):
Plant.__init__(self, ...)
...
您需要将适当的参数传递给 __init__
。第一个是self
,其余的由基数class中的Plant.__init__
定义。您还需要修正参数列表的语法,因为 `color : str' 无效 python。
注意:一般来说,更好的解决方案是调用 super 而不是对父 class __init__
进行未绑定方法调用。不过,我不确定你能用这个建议做什么。也许导师让你先继承旧的方式,然后再学习新的方式?
对于此作业,您可能应该使用 Plant.__init__(...)
,因为作业明确要求您这样做。您可能会跟进讲师询问 super
.
Bryan 的回答很完美。只为完成:
# Looks like the assignment asks for this
class Flower(Plant):
def __init__(self, name, leaves, color, petals):
# call __init__ from parent so you don't repeat code already there
Plant.__init__(self, name, leaves)
self.color = color
self.petals = petals
这是 "classic"、"non-cooperative" 继承风格,很久以前就过时了(截至 2016 年将近 15 年),因为它打破了多重继承。有关参考,请参阅 BDFL 的 post“Unifying types and classes in Python 2.2”。起初我认为这可能是一个非常古老的赋值,但我看到赋值使用 "new-style" 继承(继承自 object
是 Python 2 中新样式的签名,因为默认是旧式,在 Python 3 中只有新式)。为了使其适用于多重继承,而不是显式调用父 class(Plant.__init__
语句),我们在 Python 2 中使用这样的 super
函数:
super(Flower, self).__init__(name, leaves)
或者在 Python 3 之后(准确地说是在 PEP 0367 之后):
super().__init__(name, leaves)
即使在Python 3 中,新的继承方式是默认的,仍然鼓励您显式继承自object
。
我需要创建一个对 Plant 的 UNBOUND 方法调用以设置名称和叶子,但我不知道如何操作。感谢任何帮助。
我的代码:
class Plant(object):
def __init__(self, name : str, leaves : int):
self.plant_name = name
self.leaves = leaves
def __str__(self):
return "{} {}".format(self.plant_name, self.leaves)
def __eq__(self, plant1):
if self.leaves == plant1.leaves:
return self.leaves
def __It__(self, plant1):
if self.leaves < plant1.leaves:
print ("{} has more leaves than {}".format(plant1.plant_name, self.plant_name))
return self.leaves < plant1.leaves
elif self.leaves > plant1.leaves:
print ("{} has more leaves than {}".format(self.plant_name, plant1.plant_name))
return self.leaves < plant1.leaves
class Flower(Plant):
def __init__(self, color : str, petals : int):
self.color = color
self.petals = petals
def pick_petal(self.petals)
self.petals += 1
作业的确切措辞:
Create a new class called Flower. Flower is subclassed from the Plant class; so besides name, and leaves, it adds 2 new attributes; color, petals. Color is a string that contains the color of the flower, and petal is an int that has the number of petals on the flower. You should be able to create an init method to setup the instance. With the init you should make an UNBOUND method call to plant to setup the name and leaves. In addition, create a method called pick_petal that decrements the number of petals on the flower.
"unbound method call" 意味着您在 class 上调用方法,而不是在 class 的实例上调用方法。这意味着类似于 Plant.some_method
。
在此上下文中唯一有意义的未绑定调用是调用基 class 的 __init__
方法。好像可以满足"setup the names and leaves"的要求,以前也是比较常见的继承方式
看起来像这样:
class Flower(Plant):
def __init__(self, name, leaves, color, petals):
Plant.__init__(self, ...)
...
您需要将适当的参数传递给 __init__
。第一个是self
,其余的由基数class中的Plant.__init__
定义。您还需要修正参数列表的语法,因为 `color : str' 无效 python。
注意:一般来说,更好的解决方案是调用 super 而不是对父 class __init__
进行未绑定方法调用。不过,我不确定你能用这个建议做什么。也许导师让你先继承旧的方式,然后再学习新的方式?
对于此作业,您可能应该使用 Plant.__init__(...)
,因为作业明确要求您这样做。您可能会跟进讲师询问 super
.
Bryan 的回答很完美。只为完成:
# Looks like the assignment asks for this
class Flower(Plant):
def __init__(self, name, leaves, color, petals):
# call __init__ from parent so you don't repeat code already there
Plant.__init__(self, name, leaves)
self.color = color
self.petals = petals
这是 "classic"、"non-cooperative" 继承风格,很久以前就过时了(截至 2016 年将近 15 年),因为它打破了多重继承。有关参考,请参阅 BDFL 的 post“Unifying types and classes in Python 2.2”。起初我认为这可能是一个非常古老的赋值,但我看到赋值使用 "new-style" 继承(继承自 object
是 Python 2 中新样式的签名,因为默认是旧式,在 Python 3 中只有新式)。为了使其适用于多重继承,而不是显式调用父 class(Plant.__init__
语句),我们在 Python 2 中使用这样的 super
函数:
super(Flower, self).__init__(name, leaves)
或者在 Python 3 之后(准确地说是在 PEP 0367 之后):
super().__init__(name, leaves)
即使在Python 3 中,新的继承方式是默认的,仍然鼓励您显式继承自object
。