课后习题
请结合类的继承,为童谣定义类和方法:
小老鼠,上灯台,偷油吃,下不来,喵喵喵,猫来了,叽里咕噜滚下来。
课程代码、课件及其他相关资料地址
https://gitee.com/wilsonyin/zero-basics-python
作者回复: 我帮你补充一下super() 的定义, super() 是 Python 的一个内建函数,它用于调用父类(或多个祖先类)中的方法。这个函数在处理继承和多重继承时尤为有用。在 Python 3 中,super() 和 super(ClassName, self) 基本上是等价的,但 super() 更简洁。 class Parent: def hello(self): print("Calling parent method") class Child(Parent): def hello(self): super().hello() print("Calling child method") c = Child() c.hello() 输出结果 Calling parent method Calling child method 在上面的例子中,Child 类覆盖了 Parent 类的 hello 方法。在 Child 类的 hello 方法中,我们使用 super().hello() 调用了 Parent 类的 hello 方法。