python_day9_继承

继承

单继承

class Phone:
    id = None
    producer = "HW"

    def call_4g(self):
        print("4g通话")


class Phone2023(Phone):
    face_id = "1001"

    def call_45g(self):
        print("4.5g")


phone = Phone2023()
print(phone.producer)
phone.call_4g()
phone.call_45g()

python_day9_继承_第1张图片

多继承

class Phone:
    id = None
    producer = "HW"

    def call_4g(self):
        print("4g通话")


class Phone2023(Phone):
    face_id = "1001"

    def call_45g(self):
        print("4.5g")


class NFCreader:
    nfc_type = "瑞克5代"
    producer = "XM"

    def read_card(self):
        print("read")

    def write_card(self):
        print("write")


class R_laser:
    rc_type = "芝士海豹"

    def control(self):
        print("红外,启动")


class my_Phone(Phone, R_laser, NFCreader):
    pass  # 关键字,常用于子类


phone = my_Phone()
phone.call_4g()
phone.read_card()
phone.write_card()
phone.control()
print(phone.producer)  # my_Phone(Phone, R_laser, NFCreader),先继承Phone,所以producer为HW

python_day9_继承_第2张图片
python_day9_继承_第3张图片

你可能感兴趣的:(python,python,开发语言)