Python---多态

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

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

python 复制代码
# 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)
相关推荐
花酒锄作田1 分钟前
SQLAlchemy中使用UPSERT
python·sqlalchemy
SoleMotive.2 分钟前
一个准程序员的健身日志:用算法调试我的增肌计划
python·程序员·健身·职业转型
亓才孓10 分钟前
[Properties]写配置文件前,必须初始化Properties(引用变量没执行有效对象,调用方法会报空指针错误)
开发语言·python
Bruk.Liu15 分钟前
(LangChain 实战14):基于 ChatMessageHistory 自定义实现对话记忆功能
人工智能·python·langchain·agent
大江东去浪淘尽千古风流人物30 分钟前
【VLN】VLN(Vision-and-Language Navigation视觉语言导航)算法本质,范式难点及解决方向(1)
人工智能·python·算法
Swift社区32 分钟前
Gunicorn 与 Uvicorn 部署 Python 后端详解
开发语言·python·gunicorn
Coinsheep36 分钟前
SSTI-flask靶场搭建及通关
python·flask·ssti
IT实战课堂小元酱36 分钟前
大数据深度学习|计算机毕设项目|计算机毕设答辩|flask露天矿爆破效果分析系统开发及应用
人工智能·python·flask
码农阿豪37 分钟前
Flask应用上下文问题解析与解决方案:从错误日志到完美修复
后端·python·flask
wqq631085539 分钟前
Python基于Vue的实验室管理系统 django flask pycharm
vue.js·python·django