1.查询顺序

1)对象.属性:先从对象空间找,如果找不到,再从类空间找;再找不到,再从父类找。。

2)类名.属性:先从本类空间找,如果找不到,再从父类找。。。

注:对象与对象之间是相互独立的

#计算一个类,实例化了多少对象

class Count:
    count = 0   #此处的count不是全局变量,如果要在方法中调用,需要通过类名.变量名的方式调用
    def __init__(self):
       Count.count = Count.count + 1
c1 = Count()
c2 = Count()
c3 = Count()
c4 = Count()
c5= Count()
print(Count.count)
输出:
5
View Code

#通过类名可以修改类中的静态变量值

class Count:
    count = 0   
    def __init__(self):
       Count.count = Count.count + 1
c1 = Count()
Count.count = 20 #通过类名修改类中的静态变量值
print(Count.count)
输出:
20
View Code

#通过对象只能引用类中的静态变量值,但是不可以改变类中的静态变量值,它改变的只是类对象空间

class Count:
    count = 0
    def __init__(self):
       Count.count = Count.count + 1
c1 = Count()
c1.count = 20       #只是给对象空间,添加了一个count
print(c1.count)
print(Count.count)
print(c1.__dict__)
print(Count.__dict__)
输出:
20
1
{'count': 20}
{'__dict__': <attribute '__dict__' of 'Count' objects>, '__doc__': None, 'count': 1, '__weakref__': <attribute '__weakref__' of 'Count' objects>, '__module__': '__main__', '__init__': <function Count.__init__ at 0x0000015988ABC048>}
View Code

总结:类不可能找到对象空间中的属性,但是对象可以找到类中的属性;通过对象只能查找类中的属性,但是不能更改类中的属性(因为通过【对象.变量】只是给对象添加了一个属性,而不是给类)

2.组合

         本质就是给一个对象封装一个属性,但是组合封装的属性是另一个类的对象

1)引例:模拟英雄联盟写一个游戏人物的类(升级题).

  要求:

  ①创建一个 Game_role的类.

  ②构造方法中给对象封装name,ad(攻击力),hp(血量).三个属性.

  ③创建一个attack方法,此方法是实例化两个对象,互相攻击的功能:

   例: 实例化一个对象 盖伦,ad为10, hp为100

      实例化另个一个对象 剑豪 ad为20, hp为80

      盖伦通过attack方法攻击剑豪,此方法要完成 '谁攻击谁,谁掉了多少血,  还剩多少血'的提示功能

class GameRole:
    def __init__(self,name,ad,hp):
        self.name = name
        self.ad = ad
        self.hp = hp
    def attack(self,p):
        p.hp = p.hp-self.ad   #需要在方法中把属性改变,负责没有变化
        print("%s 攻击 %s, %s 掉了 %s 的血,还剩%s的血"% (self.name,p.name,p.name,self.ad,p.hp))

p1 = GameRole("德玛",20,400)
p2 = GameRole("亚索",40,200)
p1.attack(p2)
print(p2.hp)
输出:
德玛 攻击 亚索, 亚索 掉了 20 的血,还剩180的血
180
View Code

2)组合定义:给一个类的对象封装一个属性,这个属性是另一个类的对象

#针对上面程序,再添加一个武器类,完成X用X武器打了X,掉了X的血,还剩X的血

class GameRole:
    def __init__(self,name,ad,hp):
        self.name = name
        self.ad = ad
        self.hp = hp
    def attack(self,p):
        p.hp = p.hp-self.ad
        print("%s 攻击 %s, %s 掉了 %s 的血,还剩%s的血"% (self.name,p.name,p.name,self.ad,p.hp))
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("%s 用 %s 攻击了 %s , 掉了 %s 的血,还剩 %s 的血" % (p1.name,self.name,p2.name,self.ad,p2.hp))

p1 = GameRole("德玛",20,400)
p2 = GameRole("亚索",40,200)

bloodDrinkingSword = Weapon("饮血剑",60)
endlessBlade = Weapon("无尽之刃",100)
bloodDrinkingSword.fight(p1,p2)
endlessBlade.fight(p2,p1)
print(p1.hp,p2.hp)
输出:
德玛 用 饮血剑 攻击了 亚索 , 掉了 60 的血,还剩 140 的血
亚索 用 无尽之刃 攻击了 德玛 , 掉了 100 的血,还剩 300 的血
300 140
View Code

对于上面程序,虽然完成了功能,但是在代码的规范上是不合理的,容易让人看不明白,因为是人物利用武器攻击别人,动作的发起者是人,而不应该是武器

#版本二(利用组合)

class GameRole:
    def __init__(self,name,ad,hp):
        self.name = name
        self.ad = ad
        self.hp = hp
    def attack(self,p):
        p.hp = p.hp-self.ad
        print("%s 攻击 %s, %s 掉了 %s 的血,还剩%s的血"% (self.name,p.name,p.name,self.ad,p.hp))
    def Equipped_with_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("%s 用 %s 攻击了 %s , 掉了 %s 的血,还剩 %s 的血" % (p1.name,self.name,p2.name,self.ad,p2.hp))

p1 = GameRole("德玛",20,400)
p2 = GameRole("亚索",40,200)

bloodDrinkingSword = Weapon("饮血剑",60)
endlessBlade = Weapon("无尽之刃",100)
p1.Equipped_with_weapon(bloodDrinkingSword) #给德玛装备了饮血剑这个对象;即把bloodDrinkingSword的对象空间传给了形参wea,即又给对象p1新增加了一个属性wea,wea的值为bloodDrinkingSword的对象空间,即最终给p1添加了饮血剑这个属性(添加的属性就是另一个类的对象)
#通过如上语句后,实际上bloodDrinkingSword就是p1.wea,即给对象的一个属性,封装了另一个对象的属性
print(bloodDrinkingSword)  #输出地址值<__main__.Weapon object at 0x000001EA35C78C18>
print(p1.wea)               #输出地址值<__main__.Weapon object at 0x000001EA35C78C18>
print(p1.wea.name,p1.wea.ad)  #输出饮血剑 60
p1.wea.fight(p1,p2)
输出:
<__main__.Weapon object at 0x000001AA00398BE0>
<__main__.Weapon object at 0x000001AA00398BE0>
饮血剑 60
德玛 用 饮血剑 攻击了 亚索 , 掉了 60 的血,还剩 140 的血
View Code

3.练习

1.完成暴力摩托程序(完成下列需求)

1)创建三个游戏人物,分别

①阿狸,女,18,攻击力ad为20,血量200

②男刀,男,20,攻击力ad为30,血量150

③剑姬,女,19,攻击力ad为50,血量80

2)创建三个游戏武器,分别是:
①无尽之刃,ad为65
②饮血剑,ad为50
③饮魔刀,ad为20

3)创建三个游戏摩托车,分别是:

①小踏板,速度60迈

②雅马哈,速度80迈

③宝马,速度120迈

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

class Weapon:
    def __init__(self,name,ad):
        self.name = name
        self.ad = ad
class Motorcycle:
    def __init__(self,name,speed):
        self.name = name
        self.speed = speed
p1 = GameRole("阿狸","",18,20,300)
p2 = GameRole("男刀","",20,25,250)
p3 = GameRole("剑姬","",19,50,200)
w1 = Weapon("无尽之刃",65)
w2 = Weapon("饮血剑",50)
w3 = Weapon("饮魔刀",20)
m1 = Motorcycle("小踏板",60)
m2 = Motorcycle("雅马哈",80)
m3 = Motorcycle("宝马",120)
View Code
4)完成下列需求(利用武器打人掉的血量为武器的ad + 人的ad)

①阿狸骑着小踏板开着60迈的车行驶在赛道上。

②男刀骑着宝马开着120迈的车行驶在赛道上。

③剑姬骑着雅马哈开着80迈的车行驶在赛道上。

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 add_moto(self,mo):    #给角色装备车辆
        self.mo = mo

class Weapon:
    def __init__(self,name,ad):
        self.name = name
        self.ad = ad
class Motorcycle:
    def __init__(self,name,speed):
        self.name = name
        self.speed = speed
    def driver(self,p):
        print("%s骑着%s开着%s迈的车行驶在赛道上"%(p.name,self.name,self.speed))
p1 = GameRole("阿狸","",18,20,300)
p2 = GameRole("男刀","",20,25,250)
p3 = GameRole("剑姬","",19,50,200)
w1 = Weapon("无尽之刃",65)
w2 = Weapon("饮血剑",50)
w3 = Weapon("饮魔刀",20)
m1 = Motorcycle("小踏板",60)
m2 = Motorcycle("雅马哈",80)
m3 = Motorcycle("宝马",120)

p1.add_moto(m1)  #给对象p1装配武器m1
p1.mo.driver(p1)
p2.add_moto(m3) #给对象p2装配武器m3
p2.mo.driver(p2)
p3.add_moto(m2)#给对象p3装配武器m3
p3.mo.driver(m2)
输出:
阿狸骑着小踏板开着60迈的车行驶在赛道上
男刀骑着宝马开着120迈的车行驶在赛道上
雅马哈骑着雅马哈开着80迈的车行驶在赛道上
View Code

④阿狸赤手空拳打了男刀20滴血,男刀还剩xx血。

⑤男刀赤手空拳打了剑姬30滴血,剑姬还剩xx血。

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 finght_of_fist(self,p2):
        p2_hp = p2.hp - self.ad
        print("%s赤手空拳打了%s%s滴血,%s还剩%s血。" %(self.name,p2.name,self.ad,p2.name,p2_hp))

class Weapon:
    def __init__(self,name,ad):
        self.name = name
        self.ad = ad
class Motorcycle:
    def __init__(self,name,speed):
        self.name = name
        self.speed = speed

p1 = GameRole("阿狸","",18,20,300)
p2 = GameRole("男刀","",20,25,250)
p3 = GameRole("剑姬","",19,50,200)
w1 = Weapon("无尽之刃",65)
w2 = Weapon("饮血剑",50)
w3 = Weapon("饮魔刀",20)
m1 = Motorcycle("小踏板",60)
m2 = Motorcycle("雅马哈",80)
m3 = Motorcycle("宝马",120)

p1.finght_of_fist(p2)
p2.finght_of_fist(p3)
输出:
阿狸赤手空拳打了男刀20滴血,男刀还剩230血。
男刀赤手空拳打了剑姬25滴血,剑姬还剩175血。
View Code

⑥剑姬利用饮血剑刺了男刀一饮血剑,男刀还剩xx血。

⑦阿狸利用无尽之刃打了剑姬一无尽之刃,剑姬还剩xx血。

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 add_weapon(self,wea):
        self.wea = wea

class Weapon:
    def __init__(self,name,ad):
        self.name = name
        self.ad = ad
    def fight_of_weapon(self,p1,p2):
        p2_hp = p2.hp-p1.ad-self.ad
        print("%s利用%s打了%s一%s,%s还剩%s血" %(p1.name,self.name,p3.name,self.name,p2.name,p2_hp))
class Motorcycle:
    def __init__(self,name,speed):
        self.name = name
        self.speed = speed

p1 = GameRole("阿狸","",18,20,300)
p2 = GameRole("男刀","",20,25,250)
p3 = GameRole("剑姬","",19,50,200)
w1 = Weapon("无尽之刃",65)
w2 = Weapon("饮血剑",50)
w3 = Weapon("饮魔刀",20)
m1 = Motorcycle("小踏板",60)
m2 = Motorcycle("雅马哈",80)
m3 = Motorcycle("宝马",120)

p3.add_weapon(w2)         #给p3对象装备武器
p3.wea.fight_of_weapon(p3,p2)
p1.add_weapon(w1)       #给p3对象装备武器
p1.wea.fight_of_weapon(p1,p3)
输出:
剑姬利用饮血剑打了剑姬一饮血剑,男刀还剩150血
阿狸利用无尽之刃打了剑姬一无尽之刃,剑姬还剩115血
View Code

⑧男刀骑着宝马打了骑着小踏板的剑姬一无尽之刃,剑姬哭了,还剩xx血。

⑨剑姬骑着小踏板打了骑着雅马哈的阿狸一饮血剑,阿狸哭了,还剩xx血。

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 add_moto(self,mo):
        self.mo = mo
    def add_weapon(self,wea):
        self.wea = wea
    def fight_combination(self,p):
        p_hp = p.hp - self.ad - self.wea.ad
        print("%s骑着%s打了骑着%s的%s一%s,%s哭了,还剩%s血。" % (self.name,self.mo.name,p.mo.name,p.name,self.wea.name,p.name,p_hp))

class Weapon:
    def __init__(self,name,ad):
        self.name = name
        self.ad = ad
    def fight_of_weapon(self,p1,p2):
        p2_hp = p2.hp-p1.ad-self.ad
        print("%s利用%s打了%s一%s,%s还剩%s血" %(p1.name,self.name,p3.name,self.name,p2.name,p2_hp))
class Motorcycle:
    def __init__(self,name,speed):
        self.name = name
        self.speed = speed

p1 = GameRole("阿狸","",18,20,300)
p2 = GameRole("男刀","",20,25,250)
p3 = GameRole("剑姬","",19,50,200)
w1 = Weapon("无尽之刃",65)
w2 = Weapon("饮血剑",50)
w3 = Weapon("饮魔刀",20)
m1 = Motorcycle("小踏板",60)
m2 = Motorcycle("雅马哈",80)
m3 = Motorcycle("宝马",120)


#男刀骑着宝马打了骑着小踏板的剑姬一无尽之刃,剑姬哭了,还剩xx血。
p2.add_moto(m3)
p3.add_moto(m1)
p2.add_weapon(w1)
p2.fight_combination(p3)

#剑姬骑着小踏板打了骑着雅马哈的阿狸一饮血剑,阿狸哭了,还剩xx血。
p1.add_moto(m2)
p3.add_weapon(w2)
p3.fight_combination(p1)
输出:
男刀骑着宝马打了骑着小踏板的剑姬一无尽之刃,剑姬哭了,还剩110血。
剑姬骑着小踏板打了骑着雅马哈的阿狸一饮血剑,阿狸哭了,还剩200血。
View Code

2.定义一个类,计算圆的周长和面积。

from math import pi
class Circle:
    def __init__(self,r):
        self.r = r
    def perimiter(self):
        return self.r*2*pi
    def area(self):
        return self.r**2*pi
c1 = Circle(5)
print(c1.perimiter())
print(c1.area())
输出:
31.41592653589793
78.53981633974483
View Code

3. 定义一个圆环类,计算圆环的周长和面积(升级题)

from math import pi
class Ring:
    def __init__(self,r1,r2):
        self.r1 = r1
        self.r2 = r2
    def perimiter(self):
        return self.r1*2*pi + self.r2*2*pi
    def area(self):
        return self.r1**2*pi - self.r2**2*pi
c1 = Ring(10,5)
print(c1.perimiter())
print(c1.area())
输出:
94.24777960769379
235.61944901923448
View Code

4.以组合的方式,计算圆环的面积

from math import pi
class Circle:
    def __init__(self,r):
        self.r = r
    def perimiter(self):
        return self.r*2*pi
    def area(self):
        return self.r**2*pi

class Ring:
    def __init__(self,r1,r2):
        self.r1 = Circle(r1)
        self.r2 = Circle(r2)
    def perimiter(self):
        return self.r1.perimiter() + self.r2.perimiter()
    def area(self):
        return self.r1.area() - self.r2.area()
c1 = Ring(10,5)
print(c1.perimiter())
print(c1.area())
输出:
94.24777960769379
235.61944901923448
View Code