为什么使用super().__ init__我只能在父级中初始化第一个参数
不明白为什么这有效:
class Fish:
def __init__(self, first_name, last_name="Fish"):
self.first_name = first_name
self.last_name = last_name
class Trout(Fish):
def __init__(self, water = "freshwater"):
self.water = water
super().__init__(self)
terry = Trout()
# Initialize first name
terry.first_name = "Terry"
# Use parent __init__() through super()
print(terry.first_name + " " + terry.last_name)
# Use child __init__() override
print(terry.water)
但是,如果我希望每个孩子都有不同的last_name y无法删除其值,并像以后一样定义它,就像我们使用first_name一样:
class Fish:
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
class Trout(Fish):
def __init__(self, water = "freshwater"):
self.water = water
super().__init__(self)
terry = Trout()
# Initialize first name
terry.first_name = "Terry"
terry.last_name = "Crews"
# Use parent __init__() through super()
print(terry.first_name + " " + terry.last_name)
# Use child __init__() override
print(terry.water)
Error: TypeError: Fish.__init__() missing 1 required positional argument: 'last_name'
Don't understand why this works:
class Fish:
def __init__(self, first_name, last_name="Fish"):
self.first_name = first_name
self.last_name = last_name
class Trout(Fish):
def __init__(self, water = "freshwater"):
self.water = water
super().__init__(self)
terry = Trout()
# Initialize first name
terry.first_name = "Terry"
# Use parent __init__() through super()
print(terry.first_name + " " + terry.last_name)
# Use child __init__() override
print(terry.water)
But if I would like every child class to have different last_name y can't delete its value and define it later like we did with first_name:
class Fish:
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name
class Trout(Fish):
def __init__(self, water = "freshwater"):
self.water = water
super().__init__(self)
terry = Trout()
# Initialize first name
terry.first_name = "Terry"
terry.last_name = "Crews"
# Use parent __init__() through super()
print(terry.first_name + " " + terry.last_name)
# Use child __init__() override
print(terry.water)
Error: TypeError: Fish.__init__() missing 1 required positional argument: 'last_name'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果要在构造函数中设置属性,则需要实际将它们作为参数传递;您的孩子班需要接受它们作为参数,以便它可以将它们传递给父班。
如果您不打算使用构造函数来设置属性,请删除这些参数,然后将属性设置为默认值(例如
none
):您还可以在父类中提供被覆盖的默认值默认情况下,儿童班级可以被选可选论点覆盖,例如:
If you want to set the attributes in the constructor, you need to actually pass them as arguments; your child class needs to accept them as arguments so that it can pass them to the parent class.
If you don't plan to use the constructor to set the attributes, remove those parameters and just set the attributes to default values (e.g.
None
):You can also provide defaults in the parent class that are overridden by defaults in the child class that can in turn by overridden by optional arguments, e.g.:
使用
super().__ INT __()
方法时,您需要精确使用所继承的参数。即使在鳟鱼构造器内部,您也需要提及其所有参数,包括其继承的IEdef __init __(self,first_name,last_name,water =“ freshwater”)
。因此,您的鳟鱼课应该看起来像下面的代码When using
super().__init__()
method, you need to precise the parameters you are inheriting. and even inside your Trout constructor you need to mention all its parameters including the ones its inheriting i.edef __init__(self, first_name, last_name, water="freshwater")
. So your Trout class should look like the code below由于子类没有必要的信息,因此您没有初始化父级的任何论点。另外,您无需在打电话时提供自我。
You are not initialising any arguments of the parent class since child class doesn't have the necessary information. Also you don't need to provide self when calling super.