Python面向对象——多重继承

1本文的作用

一个从多个父类继承过来的子类,可以访问所有父类的功能。

2图文介绍

3代码验证

class Contact:
    all_contacts = []
    
    def __init__(self, name, email):
        self.name = name
        self.email = email
        Contact.all_contacts.append(self)
        
class MilSender:
    def send_mail(self, message):
        print("Sending mail to " + self.email)
        
class EmailableContact(Contact, MilSender):
    pass

e = EmailableContact("John Smith", "j@qq.com")

Contact.all_contacts

e.send_mail("Hello, test e-mail here.")

参考:本文参考学习《Python3 Object Oriented Programming》,根据自己理解改编,Dusty Phillips 著

 

posted @ 2018-04-13 23:38  既生喻何生亮  阅读(247)  评论(0编辑  收藏  举报