类的继承与继承的覆盖

# 继承
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
posted @ 2025-09-25 15:50  李大嘟嘟  阅读(5)  评论(0)    收藏  举报