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


'''

猜你喜欢

转载自www.cnblogs.com/zhanggaofeng/p/9574455.html
今日推荐