一. 类与类之间的关系
大千世界, 万物之间皆有规则和规律. 我们的类和对象是对⼤千世界中的所有事物进⾏归类. 那事物之间存在着相对应的关系. 类与类之间也同样如此. 在⾯向对象的世界中. 类与类中存在以下关系:我们会使用这个关系就行,不用扣这写名词
- 依赖关系
- 组合关系
- 继承关系(类的三大特性之一:继承。)
1.1 依赖关系
⾸先, 我们设计⼀个场景. 夏天到了大象很热,大象想到冰箱中. 注意. 在这个场景中, 其实是存在了两种事物的. ⼀个是⼤象, ⼤象负责整个事件的掌控者, 还有⼀个是冰箱, 冰箱负责被⼤象操纵. 通过这个我们要分出主次, 大象就是主 冰箱就是次
⾸先, 写出两个类, ⼀个是⼤象类, ⼀个是冰箱类
class Elphant: def __init__(self, name): self.name = name
<span class="token keyword">def</span> <span class="token function">open</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token triple-quoted-string string">''' 开⻔ '''</span> <span class="token keyword">pass</span> <span class="token keyword">def</span> <span class="token function">close</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token triple-quoted-string string">''' 关⻔ '''</span> <span class="token keyword">pass</span>
class Refrigerator:
<span class="token keyword">def</span> <span class="token function">open_door</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"冰箱⻔被打开了"</span><span class="token punctuation">)</span> <span class="token keyword">def</span> <span class="token function">close_door</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"冰箱⻔被关上了"</span><span class="token punctuation">)</span>
冰箱的功能非常简单, 只要会开⻔, 关⻔就⾏了. 但是⼤象就没那么简单了. 想想. ⼤象开⻔和关⻔的时候是不是要先找个冰箱啊. 然后呢? 打开冰箱⻔. 是不是打开刚才找到的那个冰箱⻔. 然后装⾃⼰. 最后呢? 关冰箱⻔, 注意, 关的是刚才那个冰箱吧. 也就是说. 开⻔和关⻔⽤的是同⼀个冰箱. 并且. ⼤象有更换冰箱的权利, 想进那个冰箱就进那个冰箱. 这时, ⼤象类和冰箱类的关系并没有那么的紧密. 因为⼤象可以指定任何⼀个冰箱. 接下来. 我们把代码完善⼀下
class Elphant: def __init__(self, name): self.name = name
<span class="token keyword">def</span> <span class="token function">open</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>obj1<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token triple-quoted-string string">''' 开⻔ '''</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'大象要开门了,默念三声,开'</span><span class="token punctuation">)</span> obj1<span class="token punctuation">.</span>open_door<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">def</span> <span class="token function">close</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token triple-quoted-string string">''' 关⻔ '''</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'大象要关门了,默念三声,关'</span><span class="token punctuation">)</span>
class Refrigerator:
<span class="token keyword">def</span> <span class="token function">open_door</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"冰箱⻔被打开了"</span><span class="token punctuation">)</span> <span class="token keyword">def</span> <span class="token function">close_door</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"冰箱⻔被关上了"</span><span class="token punctuation">)</span>
elphant1 = Elphant('大象')
haier = Refrigerator()
elphant1.open(haier)
通过上边的代码可以发现,将一个类名或对象当做参数传递给另一个函数被使用就是依赖关系
1.2 组合关系
这个最简单. 也是最常⽤的⼀种关系. 比如. ⼤家都有男女朋友. 男⼈关联着女朋友. 女⼈关联着男朋友. 这种关系可以是互相的, 也可以是单⽅⾯的.
定义类
class Boy: def __init__(self,name,girlFriend=None): self.name = name self.girlFriend = girlFriend
<span class="token keyword">def</span> <span class="token function">have_a_diner</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">if</span> self<span class="token punctuation">.</span>girlFriend<span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'%s 和 %s 一起晚饭'</span><span class="token operator">%</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>name<span class="token punctuation">,</span>self<span class="token punctuation">.</span>girlFriend<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">else</span><span class="token punctuation">:</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'单身狗,吃什么饭'</span><span class="token punctuation">)</span>
class Girl:
def init(self,name):
self.name = name
实例(创建)日天对象
b = Boy('日天') b.have_a_diner() # 此时是单身狗
# 突然有一天,日天牛逼了
b.girlFriend = '如花'
b.have_a_diner() #共进晚餐
实例(创建)wusir对象
# wusir 生下来就有女朋友 服不服 gg = Girl('小花') bb = Boy('wusir', gg) bb.have_a_diner()
# 结果嫌他有点娘,不硬,分了
bb.girlFriend = None
bb.have_a_diner()
我们了解了依赖关系和组合关系,现在来对比一下
依赖关系,将一个类或对象传递给另一个类的方法中
组合关系,将一个类的对象传递到另一个类的对象属性中
像这样的关系有很多很多. 比如. 学校和老师之间的关系.
学校和老师示例:
# 老师属于学校,必须有学校才可以工作 class School:
<span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>name<span class="token punctuation">,</span>address<span class="token punctuation">)</span><span class="token punctuation">:</span> self<span class="token punctuation">.</span>name <span class="token operator">=</span> name self<span class="token punctuation">.</span>address <span class="token operator">=</span> address
class Teacher:
<span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>name<span class="token punctuation">,</span>school<span class="token punctuation">)</span><span class="token punctuation">:</span> self<span class="token punctuation">.</span>name <span class="token operator">=</span> name self<span class="token punctuation">.</span>school <span class="token operator">=</span> school
s1 = School('北京校区','美丽的沙河')
s2 = School('上海校区','上海迪士尼旁边')
s3 = School('深圳校区','南山区')t1 = Teacher('武大',s1)
t2 = Teacher('海峰',s2)
t3 = Teacher('日天',s3)
print(t1.school.name)
print(t2.school.name)
print(t3.school.name)
但是学校也是依赖于老师的,所以老师学校应该互相依赖。
class School:
<span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>name<span class="token punctuation">,</span>address<span class="token punctuation">)</span><span class="token punctuation">:</span> self<span class="token punctuation">.</span>name <span class="token operator">=</span> name self<span class="token punctuation">.</span>address <span class="token operator">=</span> address self<span class="token punctuation">.</span>teacher_list <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token keyword">def</span> <span class="token function">append_teacher</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>teacher<span class="token punctuation">)</span><span class="token punctuation">:</span> self<span class="token punctuation">.</span>teacher_list<span class="token punctuation">.</span>append<span class="token punctuation">(</span>teacher<span class="token punctuation">)</span>
class Teacher:
<span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>name<span class="token punctuation">,</span>school<span class="token punctuation">)</span><span class="token punctuation">:</span> self<span class="token punctuation">.</span>name <span class="token operator">=</span> name self<span class="token punctuation">.</span>school <span class="token operator">=</span> school
s1 = School('北京校区','美丽的沙河')
s2 = School('上海校区','上海迪士尼旁边')
s3 = School('深圳校区','南山区')t1 = Teacher('武大',s1)
t2 = Teacher('海峰',s2)
t3 = Teacher('日天',s3)
s1.append_teacher(t1)
s1.append_teacher(t2)
s1.append_teacher(t3)
组合:将一个类的对象封装到另一个类的对象的属性中,就叫组合。
咱们设计一个游戏人物类,让实例化几个对象让这几个游戏人物实现互殴的效果。
class Gamerole: def __init__(self,name,ad,hp): self.name = name self.ad = ad self.hp = hp def attack(self,p1): p1.hp -= self.ad print('%s攻击%s,%s掉了%s血,还剩%s血'%(self.name,p1.name,p1.name,self.ad,p1.hp)) gailun = Gamerole('盖伦',10,200) yasuo= Gamerole('亚索',50,80)
#盖伦攻击亚索
gailun.attack(yasuo)
# 亚索攻击盖伦
yasuo.attack(gailun)
但是这样互相攻击没有意思,一般游戏类的的对战方式要借助武器,武器是一个类,武器类包含的对象很多:刀枪棍剑斧钺钩叉等等,所以咱们要写一个武器类。
class Gamerole: def __init__(self,name,ad,hp): self.name = name self.ad = ad self.hp = hp def attack(self,p1): p1.hp -= self.ad print('%s攻击%s,%s掉了%s血,还剩%s血'%(self.name,p1.name,p1.name,self.ad,p1.hp))
class Weapon:
def init(self,name,ad):
self.name = name
self.ad = ad
def weapon_attack(self,p1,p2):
p2.hp = p2.hp - self.ad - p1.ad
print('%s 利用 %s 攻击了%s,%s还剩%s血' %(p1.name,self.name,p2.name,p2.name,p2.hp))
接下来借助武器攻击对方:
pillow = Weapon('绣花枕头',2)
pillow.weapon_attack(meet,panky)
# 但是上面这么做不好,利用武器攻击也是人类是动作的发起者,所以不能是pillow武器对象,而是人类利用武器攻击对方
所以,对代码进行修改。
class Gamerole: def __init__(self,name,ad,hp): self.name = name self.ad = ad self.hp = hp def attack(self,p1): p1.hp -= self.ad print('%s攻击%s,%s掉了%s血,还剩%s血'%(self.name,p1.name,p1.name,self.ad,p1.hp))
<span class="token keyword">def</span> <span class="token function">equip_weapon</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>wea<span class="token punctuation">)</span><span class="token punctuation">:</span> self<span class="token punctuation">.</span>wea <span class="token operator">=</span> wea <span class="token comment"># 组合:给一个对象封装一个属性改属性是另一个类的对象</span>
class Weapon:
def init(self,name,ad):
self.name = name
self.ad = ad
def weapon_attack(self,p1,p2):
p2.hp = p2.hp - self.ad - p1.ad
print('%s 利用 %s 攻击了%s,%s还剩%s血'
%(p1.name,self.name,p2.name,p2.name,p2.hp))# 实例化三个人物对象:
meet = Gamerole('太白',10,200)
panky = Gamerole('金莲',20,50)
pillow = Weapon('绣花枕头',2)# 给人物装备武器对象。
meet.equip_weapon(pillow)
# 开始攻击
meet.wea.weapon_attack(meet,panky)
上面就是组合,只要是人物.equip_weapon这个方法,那么人物就封装了一个武器对象,再利用武器对象调用其类中的weapon_attack方法