类的继承与继承的覆盖
# 继承
class F:
def __init__(self):
self.info = "hello Father"
def fun1(self):
return self.info
class S(F):
pass
f = F()
s = S()
print(f.fun1()) # hello Father
print(s.fun1()) # hello Father
# 继承的覆盖
class F:
def __init__(self):
self.info = "hello Father"
def fun1(self):
return self.info
class S(F):
def __init__(self):
self.info = "hello Son"
def fun1(self):
return self.info
f = F()
s = S()
print(f.fun1()) # hello Father
print(s.fun1()) # hello Son

浙公网安备 33010602011771号