目录
1.封装
2.继承
复写和使用父类成员
1.封装
class phone:
__voltage = 0.5
def __keepsinglecore(self):
print("单核运行")
def callby5g(self):
if self.__voltage >= 1:
print("5g通话开启")
else:
self.__keepsinglecore()
print("不能开启5g通话")
p = phone()
p.callby5g()
2.继承
class phone:
imei = None
producer = "aa"
def call_by_4g(self):
print("4g通话")
class phone2024(phone):
face_id = 1
def call_by_5g(self):
print("5g通话")
p = phone2024()
print(p.producer)
p.call_by_4g()
如果是多继承并且有同名的,会以之前的为优先,之后的会被覆盖
class phone:
imei = None
producer = "aa"
def call_by_4g(self):
print("4g通话")
class nfc:
def read(self):
print("nfc读卡")
class phone2024(phone,nfc):
pass
#用pass表示这里是空的
p = phone2024()
print(p.producer)
p.call_by_4g()
p.read()
复写和使用父类成员
class phone:
imei = None
producer = "aa"
def call_by_4g(self):
print("4g通话")
class nfc:
def read(self):
print("nfc读卡")
class phone2024(phone,nfc):
producer = "bb"
def read(self):
print("nfc读卡功能启动")
#用pass表示这里是空的
p = phone2024()
print(p.producer)
p.read()
class phone:
imei = None
producer = "aa"
def call_by_4g(self):
print("4g通话")
class nfc:
def read(self):
print("nfc读卡")
class phone2024(phone,nfc):
producer = "bb"
def read(self):
print("nfc读卡功能启动")
#方式1
phone.call_by_4g(self)
print(phone.producer)
#方式2
super().call_by_4g()
print(super().producer)
#用pass表示这里是空的
p = phone2024()
print(p.producer)
p.read()