zoukankan      html  css  js  c++  java
  • 面向对象-子类重用父类同名方法或同名属性

    子类中派生出的新的方法重用父类的方法

    方式一:指名道姓,不依赖继承

    指名道姓的调用,不依赖继承

    class Hero:
        def __init__(self,nickname,life_value,aggresivity):
            self.nickname=nickname
            self.life_value=life_value
            self.aggersivity=aggresivity
        def attack(selfs,enemy):
            enemy.life_value -= selfs.aggersivity
    
    class Garen(Hero):
        camp='Demacia'
        def attack(selfs,enemy):
            Hero.attack(selfs,enemy)#指名道姓的调用,不依赖继承
            print("grom Garen Class")
    class Riven(Hero):
        camp='Noxus'
    g=Garen('草丛伦',100,30)
    r=Riven("锐雯雯",90,40)
    
    print(r.life_value)
    g.attack(r)
    print(r.life_value)

    子类初始化的对象调用父类方法

    class Hero:
        def __init__(self,nickname,life_value,aggresivity):
            self.nickname=nickname
            self.life_value=life_value
            self.aggersivity=aggresivity
        def attack(selfs,enemy):
            enemy.life_value -= selfs.aggersivity
    
    class Garen(Hero):
        camp='Demacia'
        def __init__(self,nickname,life_value,aggresivity,weapon):
            Hero.__init__(self,nickname,life_value,aggresivity)#指名道姓的调用,子类派生的新的方法调用父类方法,不依赖继承
            self.weapon=weapon
        def attack(selfs,enemy):
            Hero.attack(selfs,enemy)#指名道姓的调用,不依赖继承
            print("grom Garen Class")
    
    g=Garen('草丛伦',100,30,'金箍棒')

    方式二:super()方法引用父类方法,依赖继承

    class Hero:
        def __init__(self,nickname,life_value,aggresivity):
            self.nickname=nickname
            self.life_value=life_value
            self.aggersivity=aggresivity
        def attack(selfs,enemy):
            enemy.life_value -= selfs.aggersivity
    
    class Garen(Hero):
        def attack(self,enemy):
            super(Garen,self).attack(enemy)#super()依赖继承
            print("grom Garen Class")
    class Riven(Hero):
        pass
    g=Garen('草丛伦',100,30)
    r=Riven('锐雯雯',80,50)
    print(r.life_value)
    g.attack(r)
    print(r.life_value)

    class Hero:
        def __init__(self,nickname,life_value,aggresivity):
            self.nickname=nickname
            self.life_value=life_value
            self.aggersivity=aggresivity
        def attack(selfs,enemy):
            enemy.life_value -= selfs.aggersivity
    
    class Garen(Hero):
        def __init__(self,nickname,life_value,aggresivity,weapon):   
            super(Garen,self).__init__(nickname,life_value,aggresivity)#super(Garen,self)表示绑定方法,所以执行__init__无需传入self参数
            self.weapon=weapon
        def attack(self,enemy):
            super(Garen,self).attack(enemy)#super()依赖继承
            print("grom Garen Class")
    class Riven(Hero):
        pass
    g=Garen('草丛伦',100,30,"金箍棒")
    print(g.__dict__)

  • 相关阅读:
    SQLSERVER 的表分区(水平) 操作记录2
    GraphQl in ASP.NET Core
    初始认知学习 .net core 逐步加深
    C# 关于使用JavaScriptSerializer 序列化与返序列化的操作
    Nginx、IIS 相关命令
    SqlServer:查询指定表所有外键关联表信息
    centos 重启宝塔命令
    c# 根据日志中的方法信息,反射再次执行相关方法
    jackson 下载地址记录
    【设计模式】六大原则
  • 原文地址:https://www.cnblogs.com/yaya625202/p/8870280.html
Copyright © 2011-2022 走看看