2 回答

TA貢獻1833條經驗 獲得超4個贊
您需要的是實現check調用父方法的方法:
class SmartPhone:
def __init__(self, name):
self.name = name
def check(self):
print(“The phone is working properly”)
#Write your code here
class Xiaomi(SmartPhone):
def check(self):
print(f"This is Xiaomi {self.name}")
super().check()
class Huawei(SmartPhone):
def check(self):
print(f"This is Huawei {self.name}")
super().check()
f = Xiaomi(“Redmi Note 8”)
c = Huawei(“Y9”)
f.check()
print(“=========================”)
c.check()
print(“=========================”)

TA貢獻1810條經驗 獲得超4個贊
當重寫超類的方法時,您需要調用 super。這意味著,
def check(self):
print(f"This is Xiaomi {self.name}")
super.check()
// codes for overriding
添加回答
舉報