class person(): def __init__(self,a,b): self.a = a self.b = b def sociality(self): print(a,b) class son(person): def __init__(self,a,b,c): person.__init__(self,a,b) self.c = c def virtu(self): print(c)这段代码有什么问题吗
时间: 2023-06-09 18:02:01 浏览: 106
这段代码没有语法错误,但是在son类中的virtu方法中打印变量c时应该使用self.c而不是c。正确的代码应该是:
```
class person():
def __init__(self,a,b):
self.a = a
self.b = b
def sociality(self):
print(self.a,self.b)
class son(person):
def __init__(self,a,b,c):
person.__init__(self,a,b)
self.c = c
def virtu(self):
print(self.c)
```
相关问题
class Person: def __init__(self, name, age=0): self._name = name self._age = age @property def name(self): return self._name @name.setter def name(self, value): self._name = value @property def age(self): return self._age @age.setter def age(self, value): self._age = value
抱歉,由于排版原因,我之前的代码示例可能没有显示完整。以下是完整的代码示例:
```python
class Person:
def __init__(self, name, age=0):
self._name = name
self._age = age
@property
def name(self):
return self._name
@name.setter
def name(self, value):
self._name = value
@property
def age(self):
return self._age
@age.setter
def age(self, value):
self._age = value
```
请问还有其他问题吗?
from objprint import op class Animal: def __init__(self,age): self.age = age class Person(Animal): def __init__(self, age,name): super().__init__(age) self.name =name class Male(Person): def __init__(self, age, name): super(Person,self).__init__(age) self.gender = "male" m = Male(32,"Peter") super(Male,m).__init__(18,"Nora") op(m)
这段代码有语法错误,需要进行修正。在最后一行代码中,op(m)的括号前面应该加上一个空格,即应该写成op(m)。同时,代码中缺少一个闭合的大括号,因此需要在代码的最后添加一个大括号。在修正这些问题之后,代码应该如下所示:
```python
from objprint import op
class Animal:
def __init__(self, age):
self.age = age
class Person(Animal):
def __init__(self, age, name):
super().__init__(age)
self.name = name
class Male(Person):
def __init__(self, age, name):
super().__init__(age, name)
self.gender = "male"
m = Male(32, "Peter")
super(Male, m).__init__(18, "Nora")
op(m)
```
值得注意的是,这段代码中的super()函数的使用存在一些问题。在Male类中,应该使用super().__init__(age, name)来调用父类Person的构造函数,而不是使用super(Person, self).__init__(age),因为这样会导致TypeError异常的产生。
阅读全文