您好,登录后才能下订单哦!
python很多语法支持多态,多态的实现方法,实现接口重用
class Animal:
def __init__(self,name):
self.name=name
def talk(self):
pass
class Cat(Animal):
def talk(self):
print("%s Meow..."%self.name)
class Dog(Animal):
def talk(self):
print("%s Woof! woof!"%self.name)
d = Dog("chenronghua")
#d.talk()
c=Cat("xuliangwei")
#c.talk()
def animal_talk(obj):
obj.talk()
animal_talk(c)
animal_talk(d)
-------------------------------------
class Animal:
def __init__(self,name):
self.name=name
def talk(self):
pass
@staticmethod #装饰器
def animal_talk(obj):
obj.talk()
class Cat(Animal):
def talk(self):
print("%s Meow..."%self.name)
class Dog(Animal):
def talk(self):
print("%s Woof! woof!"%self.name)
d = Dog("chenronghua")
#d.talk()
c=Cat("xuliangwei")
#c.talk()
Animal.animal_talk(c)
Animal.animal_talk(d)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。