Python中如何利用继承实现多态?
- 内容介绍
- 文章标签
- 相关推荐
本文共计138个文字,预计阅读时间需要1分钟。
python使用Python的继承和多态特性,测试一个动物类def run_twice(animal): animal.run() animal.run()
class Animal(object): def run(self): print(Animal is running)
Python继承与多态,程序测试一个动物类,两个子类Dog、Cat。
def run_twice(animal): animal.run() animal.run() class Animal(object): def run(self): print(‘Animal is running‘) #动物类 class Dog(Animal): def run(self): print(‘dog is Running...‘) def eat(self): print(‘Eating meat...‘) #狗类 class Cat(Animal): def run(self): print(‘Cat is running‘) #猫类 dog = Dog() dog.run() dog.eat() cat = Cat() cat.run() run_twice(Dog())
读书和健身总有一个在路上
本文共计138个文字,预计阅读时间需要1分钟。
python使用Python的继承和多态特性,测试一个动物类def run_twice(animal): animal.run() animal.run()
class Animal(object): def run(self): print(Animal is running)
Python继承与多态,程序测试一个动物类,两个子类Dog、Cat。
def run_twice(animal): animal.run() animal.run() class Animal(object): def run(self): print(‘Animal is running‘) #动物类 class Dog(Animal): def run(self): print(‘dog is Running...‘) def eat(self): print(‘Eating meat...‘) #狗类 class Cat(Animal): def run(self): print(‘Cat is running‘) #猫类 dog = Dog() dog.run() dog.eat() cat = Cat() cat.run() run_twice(Dog())
读书和健身总有一个在路上

