06 ### 面向对象应用场景

def send_email(to,subject,content):
    pass
def send_dingding(to,subject,content):
    pass
def send_wechat(to,subject,content):
    pass

send_email('xxx@qq.com','cpu告警',"cpu占用率超过99%")
send_email('dingding1','cpu告警',"cpu占用率超过99%")
send_email('暴雪','cpu告警',"cpu占用率超过99%")

#通过这种进行发送,发现,有的值是相同的,此时可以使用面向对象来进行实现

class Message:
    def __init__(self,subject,content):
        self.subject = subject
        self.content = content
    def send_email(self,to):
        print(self.subject,self.content,to)
        
    def send_dingding((self,to):
        print(self.subject,self.content,to)
                      
    def send_wechat((self,to):
        print(self.subject,self.content,to)
                    
obj = Message('cpu告警',"cpu占用率超过99%")    # 将'cpu告警',"cpu占用率超过99%" 封装到对象中
obj.send_emial('xxx@live.com')  # 封装后,对象中就有值了,就可以通过self.subject 进行获取对象中的值

![](https://img2024.cnblogs.com/blog/1749876/202409/1749876-20240927061034039-398515462.png)
posted @ 2024-09-27 06:09  jhchena  阅读(4)  评论(0编辑  收藏  举报