python伪多态

class Animal:
def run(self):
print('------animal run-------')


class Cat(Animal):
def run(self):
print('------cat run-------')


class Dog(Animal):
def run(self):
print('------dog run-------')


class Pig(Animal):
pass


def func(obj_an: Animal):
obj_an.run()


animal = Animal()
cat = Cat()
dog = Dog()
pig = Pig()
func(animal)
func(cat)
func(dog)
func(pig)

print(Animal.__dict__)
print(Cat.__dict__)

输出:

------animal run-------
------cat run-------
------dog run-------
------animal run-------
{'__module__': '__main__', 'run': <function Animal.run at 0x7fe670d99a60>, '__dict__': <attribute '__dict__' of 'Animal' objects>, '__weakref__': <attribute '__weakref__' of 'Animal' objects>, '__doc__': None}
{'__module__': '__main__', 'run': <function Cat.run at 0x7fe6762a5b80>, '__doc__': None}

posted @ 2022-04-22 22:39  狒狒桑  阅读(23)  评论(0编辑  收藏  举报