Sor*_*rif 1 python inheritance super
我有这些课程.
Person
是父类,Student
是子类:
class Person(object):
def __init__(self, name):
self.name = name
class Student(Person):
def __init__(self, avr, name):
self.avr = avr
super(Student, self).__init__(self, name)
Run Code Online (Sandbox Code Playgroud)
当我尝试创建一个实例时,我收到此错误Student
:
__init__() takes exactly 2 arguments (3 given)
Run Code Online (Sandbox Code Playgroud)
我的代码出了什么问题?
如果您使用的是super,则不会传递self
给目标方法.它是隐式传递的.
super(Student, self).__init__(name)
Run Code Online (Sandbox Code Playgroud)
总共有两个论点(自我,名字).当你通过时self
,总共3个(自我,自我,名字).