类的组合,不同类的对象相互访问
class Gamerole:
def __init__(self, nickname, ad, hp):
self.nickname = nickname
self.ad = ad
self.hp = hp
def attack(self, role):
role.hp = role.hp - self.ad
print('{0}攻击了{1}, {1}掉了{2}血,还剩{3}血'.format(self.nickname, role.nickname, self.ad, role.hp))
def Transform(self, w, role2): # 这个函数试着把两个类中的对象组合,执行时主语体现在第一个。效果和w1.fight(gailun, minjun)执行结果一样,
self.w = w # 所以只要把这个函数转换为以上函数就行了,首先把我w1和EquipentArms的对象联系起来
return w.fight(self, role2) # 然后返回该类中的一个fight方法
class EquipmentArms(): # 定义一个武器的类
def __init__(self, name, ad): # 每次都会执行此函数初识化对象
self.name = name
self.ad = ad
def fight(self, role1, role2):
role2.hp = role2.hp - role1.ad
print('{}用{}攻击了{},{}掉了{}血,还剩{}血'.format(role1.nickname, self.name,\
role2.nickname, role2.nickname, role1.ad,\
role2.hp))
gailun = Gamerole('盖伦', 20, 200)
minjun = Gamerole('敏君', 10, 1000)
w1 = EquipmentArms('尚方宝剑', 30)
w2 = EquipmentArms('铁锤', 20)
gailun.Transform(w1, minjun)
# print(minjun.nickname)
# gailun.attack(minjun)
# w1.fight(gailun, minjun) # 执行结果:盖伦用尚方宝剑攻击了敏君,敏君掉了200血,还剩960血
# 武器放在前面有点不合常理,应该把主语放在第一个,在语法看比较合适,
原理
三个类中的对象相互访问,类的组合 例:
class GameRole: # 定义一个游戏角色的模板,框架
def __init__(self, name, sex, age, ad, hp): # 只要调用类,自动执行此方法,封装给角色属性。
self.name = name
self.sex = sex
self.age = age
self.ad = ad
self.hp = hp
def action1(self, motor): # 定义方法,涉及两个类,第一个是游戏角色对象本身,第二个要传摩托车类的一个对象 # 类的组合
print('{}骑着{}开着{}的速度行驶在赛道上。'. \
format(self.name, motor.name, \
motor.speed)) # 格式化输出,输出的内容自己定义,输出的格式是字符串,传给format函数参数。
def action2(self, role): # 一个类中两个对象对象的组合
role.hp -= self.ad # 为了程序的连贯性,要重新给被打的角色的血量这一属性重新赋值。
print('{}赤手空拳打了{}{}滴血,{}还剩{}血'.format(self.name, role.name, self.ad, role.name, role.hp))
def action3(self, role, weapon): # 一般涉及几个类中的对象的组合,最好把主语放在第一位。比较符合逻辑,所以在主语的类中定义方法
role.hp -= weapon.ad # 计算被打对象剩余血量
print('{}利用{}打了{}一{}, {}还剩{}血'.format(\
self.name, weapon.name, role.name, weapon.name, role.name, role.hp ))
def action4(self, role, motorcycle1, motorcycle2, weapon): # 三个类中的角色相互访问
role.hp -= weapon.ad
print('{}骑着{}打了骑着{}的{}一{},{}哭了,还剩{}血'.format(self.name,\
motorcycle1.name, motorcycle2.name,\
role.name, weapon.name, role.name,\
role.hp))
class GameWeapon: # 定义一个游戏武器的类
def __init__(self, name, ad):
self.name = name
self.ad = ad
class GameMotorcycle: # 定义摩托车总的类
def __init__(self, name, speed):
self.name = name
self.speed = speed
role1 = GameRole('苍井井', 'female', 18, 20, 200) # 调用了GameRole类模板,创建了role1具体的对象
role2 = GameRole('东尼木木', '男', 20, 30, 150) # 实例化对象的过程,以及这个对象包含的属性
role3 = GameRole('波多多', '女', 19, 50, 80)
weapon1 = GameWeapon('平底锅', 20) # 引用了武器的模板创建3个游戏武器
weapon2 = GameWeapon('斧子', 50) # __init__ 有多少个形参就传多少个实参
weapon3 = GameWeapon('双节棍', 50) # 对象没有方法,只有传进去的属性
motorcycle1 = GameMotorcycle('小踏板', 60) # 实例化三个游戏摩托车
motorcycle2 = GameMotorcycle('雅马哈', 80)
motorcycle3 = GameMotorcycle('宝马', 120)
role1.action1(motorcycle1) # 调用了两个类
role2.action1(motorcycle3) # 调用了action1方法,开辟了临时命名空间
role3.action1(motorcycle2)
role1.action2(role3) # 两个游戏角色的组合
role2.action2(role3)
role3.action3(role2, weapon2)
role1.action4(role2, motorcycle3, motorcycle1, weapon3) # 三个类中的对象组合
role3.action4(role2, motorcycle1, motorcycle2, weapon2)
python内存运行机制
例
class Circle: # 定义一个圆的类
circumference_rate = 3.14159265 # 把圆周率定义为圆的一个属性
def __init__(self, radius): # 初始化一个圆的对象时传入半径
self.radius = radius
def circle_ference(self): # 定义计算圆周长的方法
print(round(2 * self.radius * self.circumference_rate, 2)) # 类中的属性调用方法,利用类指针去引用
def circle_area(self):
print(round((self.circumference_rate * self.radius) ** 2, 2)) # 定义计算圆面积的方法
circle1 = Circle(3)
circle1.circle_ference()
circle2 = Circle(5) # 半径为5的实例化的一个对象
circle2.circle_area() # 调用类的方法计算面积
class Ring:
'''
Define a Ring class
'''
circumference_rate = 3.14 # 定义圆周率属于圆环这个类的属性
def __init__(self, radiuss, radiusb): # 初识化对象时自动执行此方法,传入的是小圆和大圆的半径
self.radiuss = radiuss
self.radiusb = radiusb
def ring_pirimeter(self,): # 定义计算圆环周长的方法
s = 2 * self.radiuss * self.circumference_rate # 对象引用类里面的元素。
b = 2 * self.radiusb * self.circumference_rate
print(s + b)
def ring_area(self): # 定义类中圆环面积的方法
sa = (self.radiuss * self.circumference_rate) ** 2 # 小圆的面积
ba = (self.radiusb * self.circumference_rate) ** 2 # 大圆的面积
print(ba - sa)
ring1 = Ring(4, 7) # 实例化ring1
ring1.ring_pirimeter() # 计算ring1的周长
ring2 = Ring(5, 9) # 实例化ring2
ring2.ring_area() # 计算ring2的面积