Python基础语法(十)多态

多态

定义的时候不清楚调用哪个方法,只有执行的时候才确定调用哪个类的方法

class Dog(object):
    def print_self(self):
        print("大家好,我是xxxx,希望以后大家多多关照....")

class Xiaotq(Dog):
    def print_self(self):
        print("hello everybody, 我是你们的老大,我是xxxx")

def introduce(temp):
    temp.print_self()

dog1 = Dog()
dog2 = Xiaotq()

introduce(dog1)
introduce(dog2)

当定义introduce方法时,并不知道调用那个对象的print_self方法,只有当执行的时候

你可能感兴趣的:(Python基础语法(十)多态)