码迷,mamicode.com
首页 > 编程语言 > 详细

Python 类的多态

时间:2018-09-02 18:34:29      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:存在   没有   def   some   继承   形式   style   class   nim   

#python的多态


class Dog():
        def eat(self):
                print("i am dog , eat something . ")


class Cat():
        def eat(self):
                print("i am cat , eat somthing .")


#多态的调用形式
def animal_eat(one):
        one.eat()




d = Dog()

c = Cat()

animal_eat(d)

animal_eat(c)

‘‘‘
python本身属于弱类型语言,变量本身没有类型
python的多态并非是里氏替换原则,仅仅是因为这个对象恰好有这个方法就行,甚至不需要存在继承


‘‘‘

 

Python 类的多态

标签:存在   没有   def   some   继承   形式   style   class   nim   

原文地址:https://www.cnblogs.com/zhanggaofeng/p/9574455.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!