ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

面向对象-继承

2022-06-24 18:33:17  阅读:162  来源: 互联网

标签:__ name 继承 self 面向对象 score age def


一、什么是继承?

继承就是一种新建子类的方式,新建的子类/派生类,被继承的类叫做父类/基类。

子类可以继承父类所有的方法以及属性。

二、为什么要有继承?

为了解决定义多个类时,代码冗余的问题。当我们在定义多个存在相同属性与功能的类时,相同的代码可能会复写多次,可以将相同的代码放到一个公共类当中,也就是父类当中,其余的类来继承父类即可。

三、子类继承了父类,是继承了什么?

1、实例属性

2、变量(数据属性)

3、调用父类的方法

A:子类继承父类的实例属性,有两种方法:

第一种:

父类名.__init__(self, 父类里的参数1, 父类里的参数2)

如下图所示:

class Father(object):
    def __init__(self,name,age):
        self.name=name
        self.age=age


class Son(Father):
    def __init__(self,name,age,score):
        self.score = score
        #子类继承父类
        Father.__init__(self, name, age)

    def show(self):
        print("My name is {0},and I am {1} years old,and my score is {2}.".format(self.name,self.age,self.score))

son=Son(name="Lucy",age=23,score=45) #实例属性
son.show()

运行结果如下:

My name is Lucy,and I am 23 years old,and my score is 45.

Process finished with exit code 0

第二种:

super().__init__(父类里的参数1,父类里的参数2)

如下图所示:

class Father(object):
    def __init__(self,name,age):
        self.name=name
        self.age=age


class Son(Father):
    def __init__(self,name,age,score):
        self.score=score
        #子类继承父类
        super().__init__(name,age)

    def show(self):
        print("My name is {0},and I am {1} years old,and my score is {2}.".format(self.name,self.age,self.score))

son=Son(name="Lucy",age=23,score=45) #实例属性
son.show()

运行结果如下:

My name is Lucy,and I am 23 years old,and my score is 45.

Process finished with exit code 0

B:子类继承父类的数据属性以及调用父类的方法

如下图所示:

class Father(object):
    address="xi'an"     #数据属性(类里面的变量),全局变量

    def __init__(self,name,age):
        self.name=name
        self.age=age

    def info(self):
        print("this is a father method.")


class Son(Father):
    def __init__(self,name,age,score):
                self.score=score
        #子类继承父类
        super().__init__(name,age)

    def show(self):
        print("My name is {0},and I am {1} years old,and my score is {2}.".format(self.name,self.age,self.score))

son=Son(name="Lucy",age=23,score=45) #实例属性
son.show()
print(son.address) #子类可以继承父类的数据属性
son.info()    #父类有的方法,子类都可以任意调用

运行结果如下:

My name is Lucy,and I am 23 years old,and my score is 45.
xi'an
this is a father method.

四、方法重写(方法覆盖)

⼦类继承⽗类后,可以调⽤⽗类的⽅法,但是由于⽗类的⽅法⽆法满足子类的要求,⼦类可以重写⽗类的⽅法。

class Father(object):
    def __init__(self,name,age):
        self.name=name
        self.age=age

    def info(self):
        print("this is a father method.")


class Son(Father):
    def __init__(self,name,age,score):
        #子类继承父类
        super().__init__(name,age)
        self.score=score

    def show(self):
        print("My name is {0},and I am {1} years old,and my score is {2}.".format(self.name,self.age,self.score))

    def info(self): #子类特有的方法,方法重写
        print("This is a son method.")

son=Son(name="Lucy",age=23,score=45) #实例属性
son.show()
son.info()   #虽然子类和父类的方法名相同,但是优先考虑的是子类的方法。

运行结果如下:

My name is Lucy,and I am 23 years old,and my score is 45.
This is a son method.

Process finished with exit code 0

五、单继承

单个类继承:子类会重写父类的方法。就如上图所示。

六、多继承

多个类继承:子类继承多个类,如下图所示:

class Father(object):
    def __init__(self,name,age):
        self.name=name
        self.age=age

    def funM(self):
        print("father")


class Mother(object):
    def funM(self):
        print("mother")

class Son(Father,Mother): #子类继承多个类,从左到右 def __init__(self,name,age,score): self.score = score #子类继承父类 super().__init__(name,age) print("My name is {0},and I am {1} years old,and my score is {2}.".format(self.name,age,self.score)) son=Son(name="Lucy",age=23,score=45) #实例属性 son.funM() #有“Father,Mother”两个父类,都是一样的方法名funM(self),那么它只能选择一个,这种选择就是从左到右,即线性查找 print(Son.mro())

运行结果如下:

My name is Lucy,and I am 23 years old,and my score is 45.
father
[<class '__main__.Son'>, <class '__main__.Father'>, <class '__main__.Mother'>, <class 'object'>]

Process finished with exit code 0

注意事项:

事项一:

class Person(object):
    def funM(self):
        print("father")

class Father(Person):
    pass

class Mother(Person):
    def funM(self):
        print("mother")

class Son(Father):
    def __init__(self,score):
        self.score=score

son=Son(score=45)
son.funM()
print(Son.mro())

运行结果如下:

father
[<class '__main__.Son'>, <class '__main__.Father'>, <class '__main__.Person'>, <class 'object'>]

Process finished with exit code 0

由运行结果分析:我们假设Person类是”祖父“,Father类是”父亲“,Son类是”儿子“

儿子继承父亲里的所有方法,而父亲又继承祖父里所有的方法,即儿子=父亲,父亲=祖父,因此,儿子=祖父,成立!

事项二-1:

class Person(object):
    def func(self):
        print("zdxf")

class Father(Person):
    def funM(self):
        print("father")

class Mother(Person):
    def funM(self):
        print("mother")

class Son(Person,Father):
    def __init__(self,score):
        self.score=score
son=Son(score=45)
print(Son.mro())
son.funM()
son.func()

运行结果:会报错

TypeError: Cannot create a consistent method resolution
order (MRO) for bases Person, Father

事项二-2:

class Person(object):
    pass

class Father(Person):
    def funM(self):
        print("father")

class Mother(Person):
    def funM(self):
        print("mother")

class Son(Father,Person):
    def __init__(self,score):
        self.score=score
son=Son(score=45)
print(Son.mro())
son.funM()

运行结果:

[<class '__main__.Son'>, <class '__main__.Father'>, <class '__main__.Person'>, <class 'object'>]
father

Process finished with exit code 0

七、继承原理

在Python的类继承中,其实存在⼀个方法解析MRO的列表,它其实就是所有基类的线性顺序列表,通过它我们可以知道继承的顺序。

如上图所示:”print(Son.mro())“,就是使用MRO来解析继承的顺序。

上述运行结果:

[<class '__main__.Son'>, <class '__main__.Father'>, <class '__main__.Mother'>, <class 'object'>

所以在Python中,基于MRO的解析顺序规则,就会从左到右开始查找基类,如果找到第⼀个匹配的属性类,就停⽌查找,如果没有,那就继续查找,直到查找到符合要求的为⽌。MRO其实就是通过⼀个C3线性化算法来实现的,它的核心思想是:

1、⼦类会优先于⽗类检查

2、多个⽗类会根据它们在列表中的顺序被依次检查

3、如果对下⼀个类存在两个合法的选择,只能选择第⼀个,线性查找

八、Python中继承遗留的历史问题

Python2:深度优先

Python3:广度优先

如下图所示:

class A:
    def show(self):
        print('A')

class B(A):
    pass

class C(A):
    def show(self):
        print('C')

class D(B,C):
    pass

if __name__ == '__main__':
    obj=D()
obj.show()

从Python2(深度优先)的角度来进行分析:D-->B-->A,因此会输出A

从Python3(广度优先)的角度来进行分析:D-->B-->A-->C,因此会输出C

 

标签:__,name,继承,self,面向对象,score,age,def
来源: https://www.cnblogs.com/wangwt123/p/16405906.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有