Python面向对象特性之继承

继承,顾名思义,继承于某一个东西,Python作为面向对象自然也有三大特性——封装、继承和多态。

#继承,Contact继承object,
class Contact:
    all_contacts = []
    def __init__(self,name,email):
        self.name = name
        self.email = email
        Contact.all_contacts.append(self)

class Supplier(Contact):
    def order(self, order):
        print("if this were a real system we would send {} order to {}".format(order, self.name))
        return order

c = Contact("some", "[email protected]")
print(c.name,c.email, c.all_contacts)
d = Supplier("some", "[email protected]")
print(d.name,d.email,d.all_contacts)
print(d.order("I need suppliers"))

运行结果是:

some some@email.com [<__main__.Contact object at 0x7f174ad763c8>]
some some@email.com [<__main__.Contact object at 0x7f174ad763c8>, <__main__.Supplier object at 0x7f174917c7b8>]
if this were a real system we would send I need suppliers order to some
I need suppliers

Process finished with exit code 0

Ref:
1、https://blog.csdn.net/jay_youth/article/details/81158277
2、Python3

猜你喜欢

转载自blog.csdn.net/woai8339/article/details/82152272