class phone: IMEI = None producer = "ITCAST" def call_by_5g(self): print("使用5g通信") class myphone(phone): producer = "ITCAST2" def call_by_5g(self): print("开启cpu单核模式") print(f"父类的厂商是:{phone.producer}") phone.call_by_5g(self) # print(f"父类厂商{super().producer}") # super.call_by_5g() print("关闭cpu单核模式") phone = myphone() phone.call_by_5g() print(phone.producer) TypeError: myphone.call_by_5g() takes 1 positional argument but 2 were given
时间: 2023-06-23 16:10:27 浏览: 126
Consumer-Producer.rar_Java Consumer_java 生产者 消费者_producer java_互
这个错误是因为在调用 `phone.call_by_5g(self)` 时,将 `self` 参数传递给了它,导致参数个数不匹配。可以直接调用 `super().call_by_5g()` 来避免这个问题。修改后的代码如下:
```
class phone:
IMEI = None
producer = "ITCAST"
def call_by_5g(self):
print("使用5g通信")
class myphone(phone):
producer = "ITCAST2"
def call_by_5g(self):
print("开启cpu单核模式")
print(f"父类的厂商是:{super().producer}")
super().call_by_5g()
print("关闭cpu单核模式")
phone = myphone()
phone.call_by_5g()
print(phone.producer)
```
阅读全文