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不是一个对象实例

结语

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

相关文章
|
7天前
|
Python
Python中的面向对象
【5月更文挑战第4天】面向对象编程(OOP)是Python的核心,涉及类与对象、封装、继承和多态。类是对象的模板,对象则是类的实例。例如,`Person`类有`__init__`构造方法和`greet`方法。
15 3
|
11天前
|
SQL 关系型数据库 MySQL
使用Python的pymysql库连接MySQL,执行CRUD操作
使用Python的pymysql库连接MySQL,执行CRUD操作:安装pymysql,然后连接(host='localhost',user='root',password='yourpassword',database='yourdatabase'),创建游标。查询数据示例:`SELECT * FROM yourtable`;插入数据:`INSERT INTO yourtable...`;更新数据:`UPDATE yourtable SET...`;删除数据:`DELETE FROM yourtable WHERE...`。
26 0
|
12天前
|
分布式计算 DataWorks 关系型数据库
MaxCompute产品使用合集之我需要在MaxCompute客户端添加Python第三方包,我该怎么操作
MaxCompute作为一款全面的大数据处理平台,广泛应用于各类大数据分析、数据挖掘、BI及机器学习场景。掌握其核心功能、熟练操作流程、遵循最佳实践,可以帮助用户高效、安全地管理和利用海量数据。以下是一个关于MaxCompute产品使用的合集,涵盖了其核心功能、应用场景、操作流程以及最佳实践等内容。
|
12天前
|
SQL 关系型数据库 MySQL
Python操作mysql数据库
Python操作mysql数据库
|
13天前
|
算法 Python
Python面向对象oop编程(二)
Python面向对象oop编程(二)
|
14天前
|
弹性计算 Serverless 应用服务中间件
Serverless 应用引擎操作报错合集之阿里函数计算中出现'python app.py'的错误如何解决
Serverless 应用引擎(SAE)是阿里云提供的Serverless PaaS平台,支持Spring Cloud、Dubbo、HSF等主流微服务框架,简化应用的部署、运维和弹性伸缩。在使用SAE过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
24 3
|
15天前
|
存储 人工智能 索引
Python中的嵌套字典访问与操作详解
Python中的嵌套字典访问与操作详解
22 1
|
17天前
|
关系型数据库 MySQL 数据库
Python从入门到精通:2.3.1数据库操作与网络编程:使用Python连接和操作数据库
Python从入门到精通:2.3.1数据库操作与网络编程:使用Python连接和操作数据库
|
17天前
|
存储 安全 Python
Python从入门到精通:2.2.2异常处理与文件操作:文件的打开、读取、写入和关闭操作。
Python从入门到精通:2.2.2异常处理与文件操作:文件的打开、读取、写入和关闭操作。
|
18天前
|
JSON 数据格式 索引
python 又一个点运算符操作的字典库:Munch
python 又一个点运算符操作的字典库:Munch
34 0