多态:

多个不同的类具有共同的方法f,各个类调用方法f,返回值不同。把方法f提取出来,封装为一个接口g。不同类的实例作为参数,传入接口g,得到不同返回值。

对‘多态’的理解,python

实例:

class Dog(object):
    def talk(self):
        print('汪汪汪')

class Cat(object):
    def talk(self):
        print('喵喵喵')

class Pig(object):
    def talk(self):
        print('嗡嗡嗡')

class Bird(object):
    def talk(self):
        print('叽叽喳喳')

def print_talk(obj):
    obj.talk()

d = Dog()
c = Cat()
p = Pig()
b = Bird()

print_talk(d)
print_talk(c)
print_talk(p)
print_talk(b)

相关文章:

  • 2021-11-29
  • 2022-12-23
  • 2022-12-23
  • 2021-07-07
猜你喜欢
  • 2022-12-23
  • 2022-02-28
  • 2022-12-23
  • 2021-07-10
  • 2021-05-20
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案