继承与重用性

'''
什么是继承
继承指的是类与类之间的关系,是一种什么'是'什么的关系,继承的功能之一就是用来解决代码重用问题继承是一种创建新类的方式,
在python中,新建的类可以继承一个或多个父类,父类又可以成为基类或超类,新建的类称为派生类或子类。
'''


class ParentClass1:
pass


class ParentClass2:
pass


class SubClass1(ParentClass1):
pass


class SubClass2(ParentClass1,ParentClass2):
pass


print(SubClass1.__bases__)
print(SubClass2.__bases__)
# (<class '__main__.ParentClass1'>,)
# (<class '__main__.ParentClass1'>, <class '__main__.ParentClass2'>)

# --------------------------
class Garen:
camp = 'Demacia'

def __init__(self,nickname,life_value,aggresivity):
self.nickname = nickname
self.life_value = life_value
self.aggresivity = aggresivity


def attack(self,enemy):
enemy.life_value -= self.aggresivity

class Riven:
camp = 'Noxus'

def __init__(self,nickname,life_value,aggresivity):
self.nickname = nickname
self.life_value = life_value
self.aggresivity = aggresivity

def attack(self,enemy):
enemy.life_value -= self.aggresivity

g1 = Garen('草丛伦',100,30)
r1 = Riven('可爱的锐雯',80,50)

print(r1.life_value)
g1.attack(r1)
print(r1.life_value)

# ----------------------------------------


class Hoer:
def __init__(self,nickname,life_value,aggresivity):
self.nickname = nickname
self.life_value = life_value
self.aggresivity = aggresivity


def attack(self,enemy):
enemy.life_value -= self.aggresivity


class Garen(Hoer):
camp = 'Demacia'

class Riven(Hoer):
camp = 'Noxus'


g1 = Garen('草丛伦',100,30)
r1 = Riven('可爱的锐雯',80,50)

print(r1.life_value)
g1.attack(r1)
print(r1.life_value)
上一篇:可重入函数、线程安全、volatile


下一篇:LOJ-10102(求A到B之间的割点)