类的组合

继续引用人狗大战游戏的例子

假设一个场景,人的攻击力远远弱于狗,那么就可以给人配备一个武器,这个武器weapon本身就是一个类,那么将人这个类和武器weapon这个类结合在一起,即人使用武器,这个就是类的组合

class person:
    def __init__(self,name,aggr,blood,sex):
        self.name=name
        self.aggr=aggr
        self.blood=blood
        self.sex=sex
        self.money=0

    def attack(self,dog):
        dog.blood-=self.aggr

    def get_weapon(self,weapon):
        if self.money>weapon.price:
            self.money-=weapon.price
            self.weapon=weapon#这一步就为person装备上了装备
            self.aggr+=weapon.aggr
        else:
            print('余额不足,请先充值')

class weapon:
    def __init__(self,name,aggr,njd,price):
        self.name=name
        self.aggr=aggr
        self.njd=njd
        self.price=price

    def hand18(self,dog):
        if self.njd>0:
            dog.blood-=self.aggr*2
            self.njd-=1

class dog:
    def __init__(self,name,aggr,blood,kind):
        self.name=name
        self.aggr=aggr
        self.blood=blood
        self.kind=kind

    def attack(self,person):
        person.blood-=self.aggr

Alex=person('alex',0.5,100,'不详')
Tady=dog('哮天犬',100,500,'teady')
W=weapon('打狗棒',100,3,998)
Alex.money+=1000
Alex.get_weapon(W)

print(Alex.weapon)
print(Alex.aggr)
Alex.attack(Tady)
print(Tady.blood)
Alex.weapon.hand18(Tady)#这里Alex是一个实例化对象,利用Alex调用weapon类中的hand18方法,就是一个组合
print(Tady.blood)

 

面向对象的三大特性:继承、多态、封装
组合:一个对象的属性值是另一个类的对象
比如alex.weapon是weapon类的对象

 求圆环的面积:法一

import math
class ring:
    def __init__(self,c1,c2):
        self.c1=c1
        self.c2=c2
    def area(self):
        return math.pi*(self.c1*2-self.c2*2)

    def perimeter(self):
        return 2*math.pi*self.c1

ring1=ring(4,2)
print(ring1.area())

法二:使用组合的知识来完成

import math
class circle:
    def __init__(self,r):
        self.r=r
    def area(self):
        return math.pi*self.r**2
    def perimeter(self):
        return math.pi*2*self.r

class ring:
    def __init__(self,c1,c2):
        self.outside=circle(c1)#使用了组合的知识,将circle类中的对象作为了这里面的属性
        self.inside=circle(c2)#注意面向对象初始化这里,等号右边必须是__init__(self,c1,c2)括号中传递的参数
    def area(self):
        return self.outside.area()-self.inside.area()

    def perimeter(self):
        return 2*math.pi*self.outside.perimeter()

ring1=ring(4,2)
print(ring1.area())

练习2:老师和生日的实例化

class Birthday:
    def __init__(self,year,month,day):
        self.year=year
        self.month = month
        self.day = day

class Teacher:
    def __init__(self,name,age,sex,birthday):
        self.name=name
        self.age=age
        self.sex = sex
        self.birthday=birthday

b=Birthday(2015,6,12)
T1=Teacher('jack',25,'',b)
print(T1.name)
print(T1.birthday.month)#得到老师生日的月份,以组合的形式

法二:

class Birthday:
    def __init__(self,year,month,day):
        self.year=year
        self.month = month
        self.day = day

class Teacher:
    def __init__(self,name,age,sex):#注意根本就没有生日的属性
        self.name=name
        self.age=age
        self.sex = sex
        self.birthday=Birthday(self,2015,6,12)#仅仅是在这里加入了生日的属性


T1=Teacher('jack',25,'')
print(T1.name)
print(T1.birthday.month)#得到老师生日的月份,以组合的形式

 

面向对象编程:
思想,角色
def__init__(self),这里的self就是一个初始的对象,可以理解为一个空的字典,
实例化:对象=类名()实例化,创造了一个self对象,执行init方法,返回self对象给外部
组合:表示的是什么有什么的关系

posted @ 2019-03-05 23:50  舒畅123  阅读(196)  评论(0编辑  收藏  举报