python(多态)

# Author:Sooele


# _*_coding:utf-8_*_


class Animal(object):
    def __init__(self, name):  # Constructor of the class
        self.name = name

    def talk(self):  # Abstract method, defined by convention only
        pass#raise NotImplementedError("Subclass must implement abstract method")
    @staticmethod
    def animal_talk(obj):  #多态  一个多重使用
        obj.talk()
class Cat(Animal):
    def talk(self):
        print('%s: 喵喵喵!' % self.name)


class Dog(Animal):
    def talk(self):
        print('%s: 汪!汪!汪!' % self.name)




d = Dog("cooi")
# d.talk()

c = Cat("coobbb")
# c.talk()

Animal.animal_talk(c)   #一个接口,多种使用
Animal.animal_talk(d)   #一个接口,多种使用

发表评论