python之类的多态
作者:互联网
多态:一种接口,多种实现
python没有直接可以实现多态的语法,但是可以实现多态。
# __*__ coding: utf-8 __*__# __author__ = "David.z"class Animal:def __init__(self,name): self.name = namedef talk(self):pass# raise NotImplementedError("Subclass must implement abstract method")class Cat(Animal):def talk(self):return "Meow"class Dog(Animal):def talk(self):return "Woof! Woof!"animals = [Cat("张三"), Dog("李四")]for animal in animals:print (animal.name + ':'+ animal.talk())
标签:__,animal,python,self,多态,Animal,之类,talk 来源: https://blog.51cto.com/3180829/2681095