Python继承和多态


Python继承与多态,程序测试一个动物类,两个子类Dog、Cat。

image

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())

读书和健身总有一个在路上


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM