首页 > 编程语言 > 详细

Python继承和多态

时间:2019-09-30 12:07:44      阅读:55      评论:0      收藏:0      [点我收藏+]

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

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

Python继承和多态

原文:https://www.cnblogs.com/Renqy/p/11611640.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!