Inheritance and reusability

'' ' 
What is inherited
inheritance refers to the relationship between class and class, is what kind of' is' what relationship, one of the functions of inheritance is used to solve the problem of code reuse Inheritance is a way to create a new class ,
in python, the new class can inherit one or more parent classes, and can be the parent class or superclass group, the new class is called a derived class, or subclass.
'' '


Class ParentClass1:
Pass


class ParentClass2:
Pass


class SubClass1 (ParentClass1):
Pass


class SubClass2 (ParentClass1 , ParentClass2):
Pass


Print (. SubClass1 __bases__)
Print (. SubClass2 __bases__)
# (<class'. In __main __ ParentClass1'>, )
# (<class 'in __main __. ParentClass1'>, <class 'in __main __. ParentClass2'>)

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

,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('Grass lun' , 100 , 30)
R1 = Riven ( 'pretty sharp Wen' , 80 , 50)

Print (r1.life_value)
g1.attack (R1)
Print (r1.life_value)

Guess you like

Origin www.cnblogs.com/kingforn/p/11313876.html