自学内容网 自学内容网

python调用父类同名成员

语法
在这里插入图片描述
print(f"父类的厂商是:{Phone.producer}“)
Phone.call_by_5g(self)
在这里插入图片描述
print(f"父类的厂商是:{super().producer}”)
print(f"父类的序列号是:{super().IMEI}")
super().call_by_5g()
print(“关闭CPU单核模式,确保性能”)

代码

“”"
演示面向对象:继承中
对父类成员的复写和调用
“”"

class Phone:
IMEI = None # 序列号
producer = “ITCAST” # 厂商

def call_by_5g(self):
    print("使用5g网络进行通话")

定义子类,复写父类成员

"""
演示面向对象:继承中
对父类成员的复写和调用
"""


class Phone:
    IMEI = None             # 序列号
    producer = "ITCAST"     # 厂商

    def call_by_5g(self):
        print("使用5g网络进行通话")


# 定义子类,复写父类成员
class MyPhone(Phone):
    producer = "ITHEIMA"        # 复写父类的成员属性

    def call_by_5g(self):
        print("开启CPU单核模式,确保通话的时候省电")
        # 方式1
        print(f"父类的厂商是:{Phone.producer}")
        Phone.call_by_5g(self)
        # 方式2
        print(f"父类的厂商是:{super().producer}")
        print(f"父类的序列号是:{super().IMEI}")
        super().call_by_5g()
        print("关闭CPU单核模式,确保性能")

phone = MyPhone()
phone.call_by_5g()
#print(phone.producer)

# 在子类中,调用父类成员

原文地址:https://blog.csdn.net/weixin_37788102/article/details/142737223

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!