• python之对象的继承


    摘要:本文主要介绍了python中类的继承,同样在c++中已经对继承有了一定的了解,所以主要了解使用方法即可。

    1、继承的初体验

     1 class father(object):
     2     def __init__(self,name,age):
     3         self.name=name
     4         self.age=age
     5     def show(self):
     6         print(self.name)
     7         print(self.age)
     8     def __del__(self):
     9         print('内存被收回')
    10 
    11 class son(father):    #注意继承的格式
    12     pass    #完全继承了父类,自己没有属性
    13 
    14 son1=son('Tom',18)
    15 son1.show()

     执行的结果如下:

    1 Tom
    2 18
    3 内存被收回

     2、多继承

    上面的例子其实是一个单继承的方法,下面介绍一下多继承。所谓多继承就是子类继承了多个父类。需要注意的是,发生多继承时,如果继承了多个同名的父类的方法,默认调用继承位置靠前的父类的方法。

     1 class father(object):
     2     def __init__(self):
     3         self.name='Tom'
     4         self.age=30
     5     def show(self):
     6         print(self.name)
     7         print(self.age)
     8     def __del__(self):
     9         print('内存被收回')
    10 
    11 class mother(object):
    12     def __init__(self):
    13         self.name='Lucy'
    14         self.age=29
    15     def show(self):
    16         print(self.name)
    17         print(self.age)
    18     def __del__(self):
    19         print('内存被收回')
    20 
    21 class son(father,mother):    #注意多继承的格式
    22     pass    #完全继承了父类,自己没有属性
    23 class daughter(mother,father):
    24     pass
    25 
    26 son1=son()
    27 son1.show()
    28 
    29 daughter1=daughter()
    30 daughter1.show()

    执行结果如下:

    1 Tom
    2 30
    3 Lucy
    4 29
    5 内存被收回
    6 内存被收回

    3、在子类中重写父类方法

    所谓重写父类方法,就是在子类中重新定义和父类中同样名字的方法,并且要注意的是,在将子类对象化以后,调用这个方法,就会默认调用重写的方法:

     1 class father(object):
     2     def __init__(self):
     3         self.name='Tom'
     4         self.age=30
     5     def show(self):
     6         print(self.name)
     7         print(self.age)
     8     def __del__(self):
     9         print('内存被收回')
    10 
    11 class mother(object):
    12     def __init__(self):
    13         self.name='Lucy'
    14         self.age=29
    15     def show(self):
    16         print(self.name)
    17         print(self.age)
    18     def __del__(self):
    19         print('内存被收回')
    20 
    21 class daughter(mother,father):
    22     def __init__(self):   #重写了方法
    23         self.name='King'
    24         self.age=10
    25     def show(self):
    26         print(self.name)
    27         print(self.age)
    28     def __del__(self):
    29         print('内存被收回')
    30 
    31 daughter1=daughter()
    32 daughter1.show()

    执行结果如下:

    1 King
    2 10
    3 内存被收回

    4、利用_mro_方法来了解继承关系

    这个方法在c++中是没有的。直接对类调用该方法就会打印出各个类之间的继承关系:

     1 class father(object):
     2     def __init__(self):
     3         self.name='Tom'
     4         self.age=30
     5     def show(self):
     6         print(self.name)
     7         print(self.age)
     8     def __del__(self):
     9         print('内存被收回')
    10 
    11 class mother(object):
    12     def __init__(self):
    13         self.name='Lucy'
    14         self.age=29
    15     def show(self):
    16         print(self.name)
    17         print(self.age)
    18     def __del__(self):
    19         print('内存被收回')
    20 
    21 class daughter(mother,father):
    22     def __init__(self):   #重写了方法
    23         self.name='King'
    24         self.age=10
    25     def show(self):
    26         print(self.name)
    27         print(self.age)
    28     def __del__(self):
    29         print('内存被收回')
    30 
    31 daughter1=daughter()
    32 daughter1.show()
    33 print(daughter.__mro__)

    执行结果如下:

    1 King
    2 10
    3 (<class '__main__.daughter'>, <class '__main__.mother'>, <class '__main__.father'>, <class 'object'>)
    4 内存被收回

    5、在子类中调用父类同名方法和属性

    在上面的第3小节中,我们在子类中写了和父类中同名的方法以后,继承以后就会默认调用子类的方法,但是有时候又需要从子类的对象下调用父类的方法,这个时候就需要在子类中重新定义相关方法来达到这一目的

     1 class father(object):
     2     def __init__(self):
     3         self.name='Tom'
     4         self.age=30
     5     def show(self):
     6         print(self.name)
     7         print(self.age)
     8     def __del__(self):
     9         print('内存被收回')
    10 
    11 class mother(object):
    12     def __init__(self):
    13         self.name='Lucy'
    14         self.age=29
    15     def show(self):
    16         print(self.name)
    17         print(self.age)
    18     def __del__(self):
    19         print('内存被收回')
    20 
    21 class daughter(mother,father):
    22     def __init__(self):   #重写了方法
    23         self.name='King'
    24         self.age=10
    25     def show(self):
    26         print(self.name)
    27         print(self.age)
    28     def father_show(self):  #重新写函数,为了调用父类的同名方法
    29         father.__init__(self)
    30         father.show(self)
    31     def __del__(self):
    32         print('内存被收回')
    33 
    34 daughter1=daughter()
    35 daughter1.show()
    36 daughter1.father_show()

    执行结果如下:

    1 King
    2 10
    3 Tom
    4 30
    5 内存被收回

    6、多层继承

    上面的例子都是两层继承关系,下面让我们体验一下多层继承:

     1 class father(object):
     2     def __init__(self):
     3         self.name='Tom'
     4         self.age=30
     5         self.id='12345678'
     6     def show(self):
     7         print(self.name)
     8         print(self.age)
     9         print(self.id)
    10     def __del__(self):
    11         print('内存被收回')
    12 
    13 class mother(object):
    14     def __init__(self):
    15         self.name='Lucy'
    16         self.age=29
    17     def show(self):
    18         print(self.name)
    19         print(self.age)
    20     def __del__(self):
    21         print('内存被收回')
    22 
    23 class daughter(father,mother):
    24     pass
    25 
    26 class grandson(daughter):    #这个‘孙子类’继承了子类,那么也就继承了父类
    27     pass
    28 
    29 grandson1=grandson()
    30 grandson1.show()

    执行结果如下:

    1 Tom
    2 30
    3 12345678
    4 内存被收回

    7、super()方法

    在上面的例子中,我们想要调用父类同名的方法的时候,需要重写一遍,这显然比较麻烦,所以创造了super方法,可以更加简单的调用父类的方法:

     1 class father(object):
     2     def __init__(self):
     3         self.name='Tom'
     4         self.age=30
     5         self.id='12345678'
     6     def show(self):
     7         print(self.name)
     8         print(self.age)
     9         print(self.id)
    10     def __del__(self):
    11         print('内存被收回')
    12 
    13 
    14 class daughter(father):
    15     def __init__(self):
    16         self.name='King'
    17         self.age=18
    18         self.id='23467'
    19     def show(self):
    20         print(self.name)
    21         print(self.age)
    22         print(self.id)
    23         super().__init__()
    24         super().show()
    25     def __del__(self):
    26         print('内存被收回')
    27 
    28 class grandson(daughter):
    29     def __init__(self):
    30         self.name = 'Jone'
    31         self.age = 9
    32         self.id = '2346787'
    33 
    34     def show(self):
    35         print(self.name)
    36         print(self.age)
    37         print(self.id)
    38        # super(grandson,self).__init__()   #带参数的写法
    39        # super(grandson,self).show()
    40         super().__init__()
    41         super().show()
    42 
    43     def __del__(self):
    44         print('内存被收回')
    45 
    46 grandson1=grandson()
    47 grandson1.show()

    执行结果如下:

     1 Jone
     2 9
     3 2346787
     4 King
     5 18
     6 23467
     7 Tom
     8 30
     9 12345678
    10 内存被收回

     8、python中的私有属性无法类外访问

    当父类有部分的属性或者方法不想在类外被访问时,可以把属性和方法设置成私有属性,具体的方法就是在属性和方法名前面添加双下划线:__

    class father(object):
        def __init__(self):
            self.__name='Tom'
            self.age=18
        def __show(self):
            print(self.__name)
            print(self.age)
        def __del__(self):
            print('内存被收回')
    
    class son(father):    #注意继承的格式
        pass    #完全继承了父类,自己没有属性
    
    son1=son()
    #print(son1.__name)    无法继承属性
    #son1.__show()         无法继承方法

    上面的代码执行出错!

    其实上面的原因就是,私有的属性和函数不可以在类外访问,道理和下面的例子是一样的:

     1 class father(object):
     2     def __init__(self):
     3         self.__name='Tom'
     4         self.age=18
     5     def __show(self):
     6         print(self.__name)
     7         print(self.age)
     8     def __del__(self):
     9         print('内存被收回')
    10 
    11 class son(father):    #注意继承的格式
    12     pass    #完全继承了父类,自己没有属性
    13 
    14 father1=father()
    15 print(father1.age)   #18
    16 #print(father1.__name)  无法访问

     当我们想要访问的时候,可以用类内的方法set和get来完成,这一点和c++是一样的。

  • 相关阅读:
    [LeetCode] 1474. Delete N Nodes After M Nodes of a Linked List
    [LeetCode] 1836. Remove Duplicates From an Unsorted Linked List
    [LeetCode] 1642. Furthest Building You Can Reach
    [LeetCode] 872. Leaf-Similar Trees
    [LeetCode] 1720. Decode XORed Array
    445. 两数相加 II
    83.删除链表中的重复元素
    笔试题常见的需要模板
    背包问题
    62.63 不同路径
  • 原文地址:https://www.cnblogs.com/lzy820260594/p/11811855.html
Copyright © 2020-2023  润新知