天天看点

python(类继承)

一、继承

1.单继承

  • 一个对象使用另一个对象的属性和方法,被继承的类也称父类

(1)父类与子类的方法不一样

class Father():
    def __init__(self, car):
        self.car = car
    def rich(self):
        print("有很有钱,买了一辆{}".format(self.car))
    def house(self):
        print("有很多楼")

class BigSon(Father):  # Son 类继承 Father 类,Son 类拥有了 Father 类的所有方法和属性
    def girlfriend(self):
        print("给女朋友买了一辆{}".format(self.car))

class SmallSon(Father):
    def toys(self):
        print("小儿子有{}玩具车".format(self.car))

s = SmallSon("法拉利")
s.toys()      

(2)子类拥有与父类相同的方法

  • 当子类拥有与父类相同的方法,通过子类实例调用该方法后,执行的是子类下的方法
class Mother():
    def name(self):
        print("This is my mother!")

class MySelf(Mother):
    #对父类方法重写
    def name(self):
        print("My name is XiaoMing")

M = MySelf()
M.name()

#结果如下
My name is XiaoMing      

(3)子类拥有与父类相同的方法和属性

class Teacher():
    #在父类中定义属性
    def __init__(self,name):
        self.name = name
    def Name(self):
        print("My teacher name is {} !".format(self.name))

class MySelf(Teacher):
    #对父类方法重写
    def Name(self):
        print("My name is {} !".format(self.name))

M = MySelf("XiaoWang")
M.Name()

#结果如下
My name is XiaoWang !      
"""
在子类中使用了 super() 函数调用父类方法(常用于多继承)
"""
class Teacher():
    #在父类中定义属性
    def __init__(self,name):
        self.name = name
    def Name(self):
        print("My teacher name is {} !".format(self.name))

class MySelf(Teacher):
    def __init__(self,course,name):
        super(MySelf, self).__init__(name)
        self.course = course
    #对父类方法重写
    def Name(self):
        print("My name is {} !".format(self.name))
    def Course(self):
        print("我的{}课老师的名字是{}".format(self.course,self.name))

M = MySelf("数学","Bob")
M.Name()
M.Course()

#结果如下
My name is Bob !
我的数学课老师的名字是Bob      

2.多继承

  • 多重继承就是一个子类继承多个父类
class Mother():
    def hobby(self):
        print("Mother love shopping!")

class Father():
    def work(self):
        print("Father work is Test Engineer")

class Myself(Father,Mother):
    pass

M = Myself()
M.work()
M.hobby()

#结果如下
Father work is Test Engineer
Mother love shopping!      
class Mother():
    def __init__(self,something):
        self.something = something

    def Hobby(self):
        print("Mother love {}!".format(self.something))

class Father():
    def __init__(self,work):
        self.work = work

    def Work(self):
        print("Father work is {}".format(self.work))

class Myself(Father,Mother):
    def __init__(self,work=None,something=None):
        # 注意:对于多继承来说,使用 super() 只会调用第一个父类的属性方法
        # 要想调用特定父类的构造器只能使用 "父类名.__init__(self)" 方式。如下:
        Father.__init__(self, work)
        Mother.__init__(self,something)

M = Myself("test", "shopping")
M.Work()
M.Hobby()
#我们可以用mro来查看顺序
print(Myself.mro())

#结果如下
Father work is test
Mother love shopping!
[<class '__main__.Myself'>, <class '__main__.Father'>, <class '__main__.Mother'>, <class 'object'>]      
  •  如果不同的两个父类出现了相同名称的属性或者方法,子类会继承谁的属性或者方法?
class Mother():
    def __init__(self,work):
        self.work = work
    def hobby(self):
        print("My mother work is {}.".format(self.work))

class Father():
    def __init__(self,work):
        self.work = work

    def hobby(self):
        print("My father work is {}.".format(self.work))

class Myself(Father,Mother):
    pass

M = Myself("Test")
M.hobby()

#结果如下
My father work is Test.      
#由上面实例可知如下

(1)python3中都是新式类:广度优先,从父类中查询对应的方法,查询到第一个满足的方法之后就直接返回
                object
                |
                A(object)
                |
    A_1(A) --> A_2(A)
    |
    Test(A_1, A_2)

(2)python2中的经典类:深度优先
            A
            |
    A  --> A_2(A)
    |
    A_1(A)
    |
    Test(A_1, A_2)      

作者:多测师高级讲师_郑sir

微信:ZhengYing8887

出处:https://www.cnblogs.com/ZhengYing0813/

备注:本文版权归作者所有,欢迎转载和添加作者微信探讨技术,但未经作者同意必须在文章页面给出原文链接,否则保留追究法律责任的权利。

继续阅读