zoukankan      html  css  js  c++  java
  • Python----面向对象---在子类中重用父类的属性

    一、指名道姓的方式,不依赖于继承

    示例代码如下:

     1 class Hero:
     2     def __init__(self, nick_name, life_value, aggresivity):
     3         self.nick_name = nick_name
     4         self.life_value = life_value
     5         self .aggresivity = aggresivity
     6 
     7     def attack(self, enemy):
     8         enemy.life_value -= self.aggresivity
     9 
    10 
    11 class Garen(Hero):
    12     camp = 'Demacia'
    13 
    14     def attack(self, enemy):
    15         Hero.attack(self, enemy)
    16         print('from Garen class')
    17 
    18 
    19 class Riven(Hero):
    20     camp = 'Noxus'
    21 
    22 
    23 g1 = Garen('草丛伦', 100, 30)
    24 r1 = Riven('锐雯雯', 80, 50)
    25 g1.attack(r1)
    26 print(r1.life_value)
    27 
    28 结果为:
    29 
    30 
    31 from Garen class
    32 50

    从以上结果可以看出,即执行了自己的特有方法,又执行了父类中继承的方法

    想为草丛伦增加一个weapon私有属性,代码如下:

     1 class Hero:
     2     def __init__(self, nick_name, life_value, aggresivity):
     3         self.nick_name = nick_name
     4         self.life_value = life_value
     5         self .aggresivity = aggresivity
     6 
     7     def attack(self, enemy):
     8         enemy.life_value -= self.aggresivity
     9 
    10 
    11 class Garen(Hero):
    12     camp = 'Demacia'
    13 
    14     def __init__(self, nick_name, life_value, aggresivity, weapon):
    15         self.nick_name = nick_name
    16         self.life_value = life_value
    17         self.aggresivity = aggresivity
    18         self.weapon = weapon
    19 
    20     def attack(self, enemy):
    21         Hero.attack(self, enemy)
    22         print('from Garen class')
    23 
    24 
    25 class Riven(Hero):
    26     camp = 'Noxus'
    27 
    28 
    29 g1 = Garen('草丛伦', 100, 30)
    30 r1 = Riven('锐雯雯', 80, 50)
    31 g1.attack(r1)
    32 print(r1.life_value)
    33 
    34 结果报错:
    35 
    36 Traceback (most recent call last):
    37   File "C:/Users/xu516/PycharmProjects/Python全栈开发/第三模块/面向对象编程/12 在子类中重用父类的属性.py", line 31, in <module>
    38     g1 = Garen('草丛伦', 100, 30)
    39 TypeError: __init__() missing 1 required positional argument: 'weapon'

    报错提示缺少一个weapon位置参数,加上位置参数就可以实例化成功了,

    但是发现了重复代码,怎样减少重复代码呢,可以使用

    1 Hero.__init__(self, nick_name, life_value, aggresivity)

    二、super()方法,依赖继承

    1 super(Garen, self).attack(enemy)
    1 super().__init__(nick_name, life_value, aggresivity)

    三、super也会按照mro顺序区查找属性,

    示例代码如下:

     1 class A:
     2     def f1(self):
     3         print('from A')
     4         super().f1()
     5 
     6 class B:
     7     def f1(self):
     8         print('from B')
     9         
    10 class C(A,B):
    11     pass
    12 
    13 print(C.mro())
    14 
    15 c = C()
    16 c.f1()
    17 
    18 结果为:
    19 
    20 [<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>]
    21 from A
    22 from B

    可以看出,super并没有去找父类,而是安装mro顺序查找

  • 相关阅读:
    DAY 179 在Flask中使用MongoDB:Flask-MongoEngine
    DAY 178 oracle基础
    DAY 177 mongoengine
    DAY 176 redis教程
    存储器
    cpu
    java 类文件类型
    线程池
    CopyOnWrite容器
    ConcurrentHashMap
  • 原文地址:https://www.cnblogs.com/xudachen/p/8597691.html
Copyright © 2011-2022 走看看