2024-04-27 03:02:54 发布
网友
class Person: def _init_(self): self.A=1 class Employee(Person): def _init_(self): print(A) object1=Person() object2=Employee()
实际上,除了拼写错误的构造函数外,该代码还存在多个问题
_init_
__init__
A
super().__init__()
Person.__init__(self)
self.A
这应该起作用:
class Person: def __init__(self): # misspelled self.A = 1 class Employee(Person): def __init__(self): # misspelled super().__init__() # call super constructor print(self.A) # use self.A
实际上,除了拼写错误的构造函数外,该代码还存在多个问题
_init_
方法应该是__init__
,否则它不是一个构造函数,只是一个碰巧被调用_init_
的方法,因此从未被调用过李>A
将不会被设置,例如使用super().__init__()
或Person.__init__(self)
self.A
来读取实例的字段A
;否则,它将查找名为A
的局部变量这应该起作用:
相关问题 更多 >
编程相关推荐