Python---多态

多态:多种状态,即完成某个行为时,使用不同的对象会得到不同的状态 (以父类做定义声明,以子类做实际工作)

抽象类(接口):含有抽象方法称为抽象类
抽象方法:方法体是空实现的(pass)称为抽象方法

# Animal为抽象类
class Animal:
    def speak(self):
        pass


class Dog:
    def speak(self):
        print("汪汪汪")


class Cat:
    def speak(self):
        print("喵喵喵")


def make_noise(animal: Animal):
    animal.speak()


dog = Dog()
cat = Cat()

make_noise(dog)
make_noise(cat)



# AC 为抽象类
class AC:
    def cool_wind(sel):
        pass

    def hot_wind(sel):
        pass

    def swing_l_r(sel):
        pass


class Midea_AC(AC):
    def cool_wind(sel):
        print("美的制冷")

    def hot_wind(sel):
        print("美的制热")

    def swing_l_r(sel):
        print("美的左右摆动")


class GREE_AC(AC):
    def cool_wind(sel):
        print("格力制冷")

    def hot_wind(sel):
        print("格力制热")

    def swing_l_r(sel):
        print("格力左右摆动")


def make_cool(ac: AC):
    ac.cool_wind()


midea_ac = Midea_AC()
gree_ac = GREE_AC()

make_cool(midea_ac)
make_cool(gree_ac)

你可能感兴趣的:(python)