多态:多态指的是一类事物有多种形态 多态性: class Animal: def run(self): raise AtrributeError("子类必须实现这种方法") class Person(Animal): pass p = Person() p.run() 通过父类主动抛出一个异常,告诉你子类中必须自己要写这个方法 改子类如下 class Person(Animal): def run(self): print("人跑") 再定义几个子类 def Dog
继承 类的继承机制使得子类可以继承父类中定义的方法,拥有父类的财产,比如有一个Animal的类作为父类,它有一个eat方法: class Animal(object): def __init__(self): print("Animal 构造函数调用!") def eat(self): print("Animal is eatting!") 写两个子类,Cat和Dog类,继承自Animal类,声明方法是在定义子类的时候在子类的括号内写上父类Animal: class