組み合わせ:クラスのオブジェクトの属性をカプセル化します。カプセル化された属性は別のクラスのオブジェクトです。
クラス:同じ属性とスキルを持つもののクラス
オブジェクト:クラス、特定のインスタンスの具体的な表現です
class GameRole:
def __init__(self,name,ad,hp):
self.name = name
self.ad = ad
self.hp = hp
def armament_weapon(self,wea):
self.wea = wea
class Weapon:
def __init__(self,name,ad):
self.name = name
self.ad = ad
def fight(self,p1,p2):
p2.hp = p2.hp - self.ad
print("{}用{}打了{},{}掉了{}血,还剩{}血"
.format(p1.name,self.name,p2.name,p2.name,self.ad,p2.hp))
p1 = GameRole('盖伦',20,500)
p2 = GameRole('亚索',50,200)
axe = Weapon('三板斧',60)
broadsword = Weapon('屠龙宝刀',10)
p1.armament_weapon(axe)
p1.wea.fight(p1,p2)
図に示すように、p1は機器メソッドを呼び出し、武器クラスの3軸オブジェクトを属性として機器メソッドに渡し、axはp1に指定されたwea属性になります。