python面向对象操作2(速通版)(下)

简介: python面向对象操作2(速通版)(上)

8.多层多继承时的初始化问题

菱形继承

class Person(object):
    def __init__(self,aaa):
        print("Person")
        self.aaa = aaa
class Father(Person):
    def __init__(self,aaa,name):
        Person.__init__(self,aaa)
        print("Father")
        self.name = name
class Mother(Person):
    def __init__(self,aaa,age):
        Person.__init__(self, aaa)
        print("Mother")
        self.age = age
class Son(Father,Mother):
    def __init__(self,aaa,name,age,gender):
        print("Son")
        Mother.__init__(self,aaa,age)
        Father.__init__(self,aaa,name)
        self.gender =  gender
s = Son(1,'tom',12,'男')
print("______________________________________________________")
print(s.aaa)
print(s.name)
print(s.age)
print(s.gender)

菱形继承的问题如何解决呢?

super解决

让子类调用father,father调用mather,mather调用person,变成线性的

class Person(object):
    def __init__(self,aaa):
        print("Person")
        self.aaa = aaa
class Father(Person):
    def __init__(self,aaa,name,age):
        super(Father,self).__init__(aaa,age)
        print("Father")
        self.name = name
class Mother(Person):
    def __init__(self,aaa,age):
        super(Mother,self).__init__(aaa)
        print("Mother")
        self.age = age
class Son(Father,Mother):
    def __init__(self,aaa,name,age,gender):
        print("Son")
        super(Son,self).__init__(aaa,name,age)
        self.gender =  gender
s = Son(1,'tom',12,'男')
print("______________________________________________________")
print(s.aaa)
print(s.name)
print(s.age)
print(s.gender)

super的执行过程

mro是个元组,元素的顺序是解释器定义的

注意根据这个列表我们可以得到son——father——mather——person这个顺序

如果顺序不对也会报错

class Person(object):
    def __init__(self,aaa):
        print("Person")
        self.aaa = aaa
class Father(Person):
    def __init__(self,aaa,name):
        super(Father,self).__init__(aaa)
        print("Father")
        self.name = name
class Mother(Person):
    def __init__(self,aaa,name,age):
        super(Mother,self).__init__(aaa,name)
        print("Mother")
        self.age = age
class Son(Father,Mother):
    def __init__(self,aaa,name,age,gender):
        print("Son")
        super(Son,self).__init__(aaa,name,age)
        self.gender =  gender
s = Son(1,'tom',12,'男')
print(Son.__mro__)

9.多继承初始化传参问题

class Person(object):
    def __init__(self,aaa):
        print("Person")
        self.aaa = aaa
class Father(Person):
    def __init__(self,name,*args):
        #这里不用担心传参的问题,因为这里会自动解包
        super(Father,self).__init__(*args)
        print("Father")
        self.name = name
class Mother(Person):
    def __init__(self,age,aaa):
        super(Mother,self).__init__(aaa)
        print("Mother")
        self.age = age
class Son(Father,Mother):
    def __init__(self,gender,name,age,aaa):
        print("Son")
        super(Son,self).__init__(name,age,aaa)
        self.gender =  gender
s = Son('男','tom',12,1)
print(Son.__mro__)
print(s.name)
print(s.age)
print(s.gender)
print(s.aaa)

10.super简化写法

class Person(object):
    def __init__(self,aaa):
        print("Person")
        self.aaa = aaa
class Father(Person):
    def __init__(self,name,*args):
        #这里不用担心传参的问题,因为这里会自动解包
        #super(Father,self).__init__(*args)
        super().__init__(*args)
        print("Father")
        self.name = name
class Mother(Person):
    def __init__(self,age,aaa):
        #super(Mother,self).__init__(aaa)
        super().__init__(aaa)
        print("Mother")
        self.age = age
class Son(Father,Mother):
    def __init__(self,gender,name,age,aaa):
        print("Son")
        #super(Son,self).__init__(name,age,aaa)
        super().__init__(name, age, aaa)
        self.gender =  gender
s = Son('男','tom',12,1)
print(Son.__mro__)
print(s.name)
print(s.age)
print(s.gender)
print(s.aaa)

11.影响mro的顺序

类的额继承书写顺序会影响mro的顺序,但不会改变mro的顺序

'''
多重多继承时,方法的查找顺序也参考MRO
'''
class A(object):
    pass
class B(A):
    pass
class C(A):
    pass
class D(B,C):
    pass
print(D.__mro__)

'''
多重多继承时,方法的查找顺序也参考MRO
'''
class A(object):
    pass
class B(A):
    pass
class C(A):
    pass
class D(C,B):
    pass
print(D.__mro__)

最子类的参数书写顺序会影响mro的元素顺序

class A(object):
    def show(self):
        print('A show run ...')
class B(A):
    def show(self):
        print('B show run ...')
class C(A):
    def show(self):
        print('C show run ...')
class D(C,B):
    pass
print(D.__mro__)
D().show()

class A(object):
    def show(self):
        print('A show run ...')
class B(A):
    def show(self):
        print('B show run ...')
class C(A):
    def show(self):
        print('C show run ...')
class D(B,C):
    pass
print(D.__mro__)
D().show()

12.调用父类两种方法

super调用

class A(object):
    def show(self):
        print('A show run ...')
class B(A):
    def show(self):
        print('B show run ...')
class C(A):
    def show(self):
        super().show()
        print('C show run ...')
class D(C,B):
    pass
print(D.__mro__)
D().show()

直接调用

class A(object):
    def show(self):
        print('A show run ...')
class B(A):
    def show(self):
        print('B show run ...')
class C(A):
    def show(self):
        B().show()
        print('C show run ...')
class D(C,B):
    pass
print(D.__mro__)
D().show()

或者

class A(object):
    def show(self):
        print('A show run ...')
class B(A):
    def show(self):
        print('B show run ...')
class C(A):
    def show(self):
        B.show(self)
        print('C show run ...')
class D(C,B):
    pass
print(D.__mro__)
D().show()

因为B.show()的B不是一个对象实例

结语

上班时间不多,尽量多更新,点点赞吧!

相关文章
|
2月前
|
Java 程序员 C++
Python 面向对象详解!
本文详细介绍了Python中的面向对象编程(OOP),包括类、对象、继承、封装、多态和抽象等核心概念。通过具体示例,解释了如何使用类定义对象的属性和方法,以及如何通过继承实现代码重用。文章还探讨了封装和多态的重要性,并介绍了私有属性和抽象类的使用方法。最后,总结了OOP的四大支柱:封装、抽象、继承和多态,强调了这些概念在Python编程中的应用。适合Java程序员扩展Python编程知识。
77 2
|
4月前
|
Python
你真的会面向对象吗!解密Python“魔术方法”
你真的会面向对象吗!解密Python“魔术方法”
42 0
|
2月前
|
Python
Python面向对象(2)
【10月更文挑战第14天】
Python面向对象(2)
|
2月前
|
设计模式 程序员 C语言
Python面向对象
【10月更文挑战第13天】
Python面向对象
|
3月前
|
前端开发 Python
Python编程的面向对象有哪些(二)
Python编程的面向对象(二)—类的多态
25 7
|
3月前
|
IDE Java 开发工具
Python类与面向对象
Python类与面向对象
|
2月前
|
Python
Python编程-关于面向对象的一些
Python编程-关于面向对象的一些
18 0
|
5月前
|
存储 监控 数据处理
💻Python高手必备!文件系统操作秘籍,让你的数据存取如臂使指
【7月更文挑战第29天】在数据驱动时代, Python以简洁语法、丰富库生态和强大跨平台能力, 成为数据科学等领域首选。本文探讨Python文件系统操作秘籍, 助力高效数据处理。
51 11
|
4月前
|
安全 算法 Go
Python面向对象的三大特性
python面向对象编程(OOP)的三大特性是封装、继承和多态。这些特性共同构成了OOP的基础,使得软件设计更加灵活、可维护和可扩展。
36 3