Crossin的编程教室

标题: Python类的多继承问题 [打印本页]

作者: buddy    时间: 2022-12-16 01:52
标题: Python类的多继承问题
类的单继承运行正常,代码如下:
class Phone:
    IMEI = None  # 序列号
    producer = 'Buddy'  # 厂商

    def call_by_4G(self):
        print('4G通话状态。')

class Phone2022(Phone):
    face_id = '1001'

    def call_by_5G(self):
        print('2022年新功能:5G通话。')

phone = Phone2022()
print(phone.producer)
phone.call_by_4G()
phone.call_by_5G()


但是多继承运行出错,代码如下:
class Phone:
    IMEI = None  # 序列号
    producer = 'Buddy'  # 厂商

    def call_by_4G(self):
        print('4G通话状态。')

class NFCReader:
    nfc_type = '第五代'
    producer = 'Buddy'  # 厂商

    def read_card(self):
        print('NFC读卡。')

    def write_card(self):
        print('NFC写卡。')
   
class RemoteControl():
    rc_type = '红外摇控'

    def control(self):
        print('红外摇控开启了。')

class MyPhone(Phone, NFCReader, RemoteControl):
    pass

phone = MyPhone
phone.call_by_4G()
phone.read_card()
phone.write_card()
phone.control()


出错提示:
C:\Users\Administrator\AppData\Local\Programs\Python\Python38\python.exe C:/Users/Administrator/PycharmProjects/pythonProject2/类/10_类的多继承.py
Traceback (most recent call last):
  File "C:/Users/Administrator/PycharmProjects/pythonProject2/类/10_类的多继承.py", line 28, in <module>
    phone.call_by_4G()
TypeError: call_by_4G() missing 1 required positional argument: 'self'

请教是什么原因?

作者: buddy    时间: 2022-12-16 23:01
已解决,今天重新看了一下,是实例忘记加括号了,真是粗心。。。
作者: crossin先生    时间: 2022-12-17 02:03
不加括号它就变成了类,而不是实例




欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/) Powered by Discuz! X2.5