当前位置:首页 > Python教程 > python技巧

Python 类的多态

            #
            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的多态并非是里氏替换原则,仅仅是因为这个对象恰好有这个方法就行,甚至不需要存在继承


‘‘‘

 

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


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!