类的空间问题
添加对象属性
class A:def __init__(self,name):self.name = namedef func(self,sex):self.sex = sex
Python
Copy
在类外部添加
class A:def __init__(self,name):self.name = namedef func(self,sex):self.sex = sexaobj = A('chensong')obj.age = 18print(obj.__dict__)
Python
Copy
类的内部添加
class A:def __init__(self,name):self.name = namedef func(self,sex):self.sex = sexobj = A('chensong')obj.func('男')print(obj.__dict__)
Python
Copy
总结:对象的属性不仅可以在 init 里面添加,还可以在类的其他方法或者类的外面添加。
添加类的静态属性
class A:def __init__(self,name):self.name = namedef func(self,sex):self.sex = sexdef func1(self):A.bbb = selfA.aaa = 'test' # 类的外部添加print(A.__dict__)A.func1('123') # 类的内部添加print(A.__dict__)
Python
Copy
总结:类的属性不仅可以在类内部添加,还可以在类的外部添加
对象如何找到类的属性
对象空间
- 产生这个对象空间,并有一个类对象指针
- 执行
__init__方法,给对象封装属性
对象查找属性的顺序:先从对象空间找 ———> 类空间找 ———> 父类空间找 ———->…..
类名查找属性的顺序:先从本类空间找 ———-> 父类空间找 ————> ……..
上面的顺序都是单向不可逆,类名不可能找到对象的属性。
类与类之间的关系
类与类中存在以下关系:
- 依赖关系
- 关联关系
- 组合关系
- 聚合关系
- 实现关系
- 继承关系 (类的三大特性之一:继承。)
依赖关系
例:将大象装进冰箱,需要两个类, ⼀个是⼤象类, ⼀个是冰箱类
class Elphant:def __init__(self,name):self.name = namedef open(self):'''开门'''passdef close(self):'''关门'''passclass Refrigerator:def open_door(self):print('冰箱门打开了')def open_door(self):print('冰箱门关上了')
Python
Copy
将大象类和冰箱类进行依赖
class Elphant:def __init__(self,name):self.name = namedef open(self,obj1):'''开门'''print(self.name,'要开门了')obj1.open_door()def close(self):'''关门'''passclass Refrigerator:def open_door(self):print('冰箱门打开了')def close_door(self):print('冰箱门关上了')elphant1 = Elphant('大象')haier = Refrigerator()elphant1.open(haier)
Python
Copy
关联, 聚合, 组合关系
其实这三个在代码上写法是⼀样的. 但是, 从含义上是不⼀样的.
- 关联关系. 两种事物必须是互相关联的. 但是在某些特殊情况下是可以更改和更换的.
- 聚合关系. 属于关联关系中的⼀种特例. 侧重点是 xxx 和 xxx 聚合成 xxx. 各⾃有各⾃的声明周期. 比如电脑. 电脑⾥有 CPU, 硬盘, 内存等等. 电脑挂了. CPU 还是好的. 还是完整的个体
- 组合关系. 属于关联关系中的⼀种特例. 写法上差不多. 组合关系比聚合还要紧密. 比如⼈的⼤脑, ⼼脏, 各个器官. 这些器官组合成⼀个⼈. 这时. ⼈如果挂了. 其他的东⻄也跟着挂了
关联关系
class Boy:def __init__(self,name,girlFirend=None):self.name = nameself.girlFriend = girlFirenddef have_a_dinner(self):if self.girlFriend:print('%s 和 %s 一起晚饭'%(self.name,self.girlFriend.name))else:print('单身狗,吃什么饭')class Girl:def __init__(self,name):self.name = nameb = Boy('日天')b.have_a_dinner()b.girlFriend = Girl('如花')b.have_a_dinner()gg = Girl('花花')bb = Boy('songsong',gg)bb.have_a_dinner()
Python
Copy
注意. 此时 Boy 和 Girl 两个类之间就是关联关系. 两个类的对象紧密联系着. 其中⼀个没有了. 另⼀个就孤单的不得了. 关联关系, 其实就是 我需要你. 你也属于我
学校和老师之间的关系
class School:def __init__(self,name,address):self.name = nameself.address = addressclass Teacher:def __init__(self,name,school):self.name = nameself.school = schools1 = School('北京校区','北京')s2 = School('上海校区','上海')s3 = School('深圳校区','深圳')t1 = Teacher('T1',s1)t2 = Teacher('T2',s2)t3 = Teacher('T3',s3)print(t1.school.name)print(t2.school.name)print(t3.school.name)
Python
Copy
但是学校也是依赖于老师的,所以老师学校应该互相依赖。
class School:def __init__(self,name,address):self.name = nameself.address = addressself.teacher_list = []def append_teacher(self,teacher):self.teacher_list.append(teacher)class Teacher:def __init__(self,name,school):self.name = nameself.school = schools1 = School('北京校区','北京')s2 = School('上海校区','上海')s3 = School('深圳校区','深圳')t1 = Teacher('T1',s1)t2 = Teacher('T2',s2)t3 = Teacher('T3',s3)s1.append_teacher(t1.name)s1.append_teacher(t2.name)s1.append_teacher(t3.name)print(s1.teacher_list)
Python
Copy
组合:将一个类的对象封装到另一个类的对象的属性中,就叫组合。
例:设计一个游戏,让游戏里面的人物互殴
class Gamerole:def __init__(self,name,ad,hp):self.name = nameself.ad = adself.hp = hpdef attack(self,p1):p1.hp -= self.adprint('%s攻击%s,%s掉了%s血,还剩%s'%(self.name,p1.name,p1.name,self.ad,p1.hp))man = Gamerole('人',10,100)dog = Gamerole('狗',50,100)dog.attack(man)man.attack(dog)
Python
Copy
加上一个武器类,让人使用武器攻击
class Gamerole:def __init__(self,name,ad,hp):self.name = nameself.ad = adself.hp = hpdef attack(self,p1):p1.hp -= self.adprint('%s攻击%s,%s掉了%s血,还剩%s'%(self.name,p1.name,p1.name,self.ad,p1.hp))def equip_weapon(self,wea):self.wea = weaclass Weapon:def __init__(self,name,ad):self.name = nameself.ad = addef weapon_attack(self,p1,p2):p2.hp = p2.hp - self.ad - p1.adprint('%s利用%s攻击了%s,%s还剩%s血'%(p1.name,self.name,p2.name,p2.name,p2.hp))man = Gamerole('人',10,100)dog = Gamerole('狗',50,100)stick = Weapon('木棍',40)man.equip_weapon(stick)man.wea.weapon_attack(man,dog)# 人利用木棍攻击了狗,狗还剩50血
