python_day10_多态

多态

class Animal:
    def bark(self):
        pass


class Dog(Animal):
    def bark(self):
        print("原神,启动")


class Chicken(Animal):
    def bark(self):
        print("你干嘛,哎呀")


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


dog = Dog()
chicken = Chicken()
make_noise(dog)
make_noise(chicken)

python_day10_多态_第1张图片

抽象类

python_day10_多态_第2张图片

class AC:
    def cool_wind(self):
        pass

    def hot_wind(self):
        pass

    def swing_left_right(self):
        pass


class Meidi_AC(AC):
    def cool_wind(self):
        print("美的制冷")

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

    def swing_left_right(self):
        print("美的左右摆动")


class DaJin_AC(AC):
    def cool_wind(self):
        print("大金制冷")

    def hot_wind(self):
        print("大金制热")

    def swing_left_right(self):
        print("大金左右摆动")


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


meidi_ac = Meidi_AC()
dajin_ac = DaJin_AC()

make_cool(meidi_ac)
make_cool(dajin_ac)

python_day10_多态_第3张图片
python_day10_多态_第4张图片

你可能感兴趣的:(python,python,开发语言)