类的空间问题

添加对象属性

  1. class A:
  2. def __init__(self,name):
  3. self.name = name
  4. def func(self,sex):
  5. self.sex = sex

Python
Copy
在类外部添加

  1. class A:
  2. def __init__(self,name):
  3. self.name = name
  4. def func(self,sex):
  5. self.sex = sexa
  6. obj = A('chensong')
  7. obj.age = 18
  8. print(obj.__dict__)

Python
Copy
类的内部添加

  1. class A:
  2. def __init__(self,name):
  3. self.name = name
  4. def func(self,sex):
  5. self.sex = sex
  6. obj = A('chensong')
  7. obj.func('男')
  8. print(obj.__dict__)

Python
Copy
总结:对象的属性不仅可以在 init 里面添加,还可以在类的其他方法或者类的外面添加。

添加类的静态属性

  1. class A:
  2. def __init__(self,name):
  3. self.name = name
  4. def func(self,sex):
  5. self.sex = sex
  6. def func1(self):
  7. A.bbb = self
  8. A.aaa = 'test' # 类的外部添加
  9. print(A.__dict__)
  10. A.func1('123') # 类的内部添加
  11. print(A.__dict__)

Python
Copy
总结:类的属性不仅可以在类内部添加,还可以在类的外部添加

对象如何找到类的属性

对象空间

  1. 产生这个对象空间,并有一个类对象指针
  2. 执行__init__方法,给对象封装属性

对象查找属性的顺序:先从对象空间找 ———> 类空间找 ———> 父类空间找 ———->…..
类名查找属性的顺序:先从本类空间找 ———-> 父类空间找 ————> ……..
上面的顺序都是单向不可逆,类名不可能找到对象的属性。

类与类之间的关系

类与类中存在以下关系:

  1. 依赖关系
  2. 关联关系
  3. 组合关系
  4. 聚合关系
  5. 实现关系
  6. 继承关系 (类的三大特性之一:继承。)

依赖关系

例:将大象装进冰箱,需要两个类, ⼀个是⼤象类, ⼀个是冰箱类

  1. class Elphant:
  2. def __init__(self,name):
  3. self.name = name
  4. def open(self):
  5. '''
  6. 开门
  7. '''
  8. pass
  9. def close(self):
  10. '''
  11. 关门
  12. '''
  13. pass
  14. class Refrigerator:
  15. def open_door(self):
  16. print('冰箱门打开了')
  17. def open_door(self):
  18. print('冰箱门关上了')

Python
Copy
将大象类和冰箱类进行依赖

  1. class Elphant:
  2. def __init__(self,name):
  3. self.name = name
  4. def open(self,obj1):
  5. '''
  6. 开门
  7. '''
  8. print(self.name,'要开门了')
  9. obj1.open_door()
  10. def close(self):
  11. '''
  12. 关门
  13. '''
  14. pass
  15. class Refrigerator:
  16. def open_door(self):
  17. print('冰箱门打开了')
  18. def close_door(self):
  19. print('冰箱门关上了')
  20. elphant1 = Elphant('大象')
  21. haier = Refrigerator()
  22. elphant1.open(haier)

Python
Copy

关联, 聚合, 组合关系

其实这三个在代码上写法是⼀样的. 但是, 从含义上是不⼀样的.

  1. 关联关系. 两种事物必须是互相关联的. 但是在某些特殊情况下是可以更改和更换的.
  2. 聚合关系. 属于关联关系中的⼀种特例. 侧重点是 xxx 和 xxx 聚合成 xxx. 各⾃有各⾃的声明周期. 比如电脑. 电脑⾥有 CPU, 硬盘, 内存等等. 电脑挂了. CPU 还是好的. 还是完整的个体
  3. 组合关系. 属于关联关系中的⼀种特例. 写法上差不多. 组合关系比聚合还要紧密. 比如⼈的⼤脑, ⼼脏, 各个器官. 这些器官组合成⼀个⼈. 这时. ⼈如果挂了. 其他的东⻄也跟着挂了

关联关系

  1. class Boy:
  2. def __init__(self,name,girlFirend=None):
  3. self.name = name
  4. self.girlFriend = girlFirend
  5. def have_a_dinner(self):
  6. if self.girlFriend:
  7. print('%s 和 %s 一起晚饭'%(self.name,self.girlFriend.name))
  8. else:
  9. print('单身狗,吃什么饭')
  10. class Girl:
  11. def __init__(self,name):
  12. self.name = name
  13. b = Boy('日天')
  14. b.have_a_dinner()
  15. b.girlFriend = Girl('如花')
  16. b.have_a_dinner()
  17. gg = Girl('花花')
  18. bb = Boy('songsong',gg)
  19. bb.have_a_dinner()

Python
Copy
注意. 此时 Boy 和 Girl 两个类之间就是关联关系. 两个类的对象紧密联系着. 其中⼀个没有了. 另⼀个就孤单的不得了. 关联关系, 其实就是 我需要你. 你也属于我
学校和老师之间的关系

  1. class School:
  2. def __init__(self,name,address):
  3. self.name = name
  4. self.address = address
  5. class Teacher:
  6. def __init__(self,name,school):
  7. self.name = name
  8. self.school = school
  9. s1 = School('北京校区','北京')
  10. s2 = School('上海校区','上海')
  11. s3 = School('深圳校区','深圳')
  12. t1 = Teacher('T1',s1)
  13. t2 = Teacher('T2',s2)
  14. t3 = Teacher('T3',s3)
  15. print(t1.school.name)
  16. print(t2.school.name)
  17. print(t3.school.name)

Python
Copy
但是学校也是依赖于老师的,所以老师学校应该互相依赖。

  1. class School:
  2. def __init__(self,name,address):
  3. self.name = name
  4. self.address = address
  5. self.teacher_list = []
  6. def append_teacher(self,teacher):
  7. self.teacher_list.append(teacher)
  8. class Teacher:
  9. def __init__(self,name,school):
  10. self.name = name
  11. self.school = school
  12. s1 = School('北京校区','北京')
  13. s2 = School('上海校区','上海')
  14. s3 = School('深圳校区','深圳')
  15. t1 = Teacher('T1',s1)
  16. t2 = Teacher('T2',s2)
  17. t3 = Teacher('T3',s3)
  18. s1.append_teacher(t1.name)
  19. s1.append_teacher(t2.name)
  20. s1.append_teacher(t3.name)
  21. print(s1.teacher_list)

Python
Copy
组合:将一个类的对象封装到另一个类的对象的属性中,就叫组合。
例:设计一个游戏,让游戏里面的人物互殴

  1. class Gamerole:
  2. def __init__(self,name,ad,hp):
  3. self.name = name
  4. self.ad = ad
  5. self.hp = hp
  6. def attack(self,p1):
  7. p1.hp -= self.ad
  8. print('%s攻击%s,%s掉了%s血,还剩%s'%(self.name,p1.name,p1.name,self.ad,p1.hp))
  9. man = Gamerole('人',10,100)
  10. dog = Gamerole('狗',50,100)
  11. dog.attack(man)
  12. man.attack(dog)

Python
Copy
加上一个武器类,让人使用武器攻击

  1. class Gamerole:
  2. def __init__(self,name,ad,hp):
  3. self.name = name
  4. self.ad = ad
  5. self.hp = hp
  6. def attack(self,p1):
  7. p1.hp -= self.ad
  8. print('%s攻击%s,%s掉了%s血,还剩%s'%(self.name,p1.name,p1.name,self.ad,p1.hp))
  9. def equip_weapon(self,wea):
  10. self.wea = wea
  11. class Weapon:
  12. def __init__(self,name,ad):
  13. self.name = name
  14. self.ad = ad
  15. def weapon_attack(self,p1,p2):
  16. p2.hp = p2.hp - self.ad - p1.ad
  17. print('%s利用%s攻击了%s,%s还剩%s血'%(p1.name,self.name,p2.name,p2.name,p2.hp))
  18. man = Gamerole('人',10,100)
  19. dog = Gamerole('狗',50,100)
  20. stick = Weapon('木棍',40)
  21. man.equip_weapon(stick)
  22. man.wea.weapon_attack(man,dog)
  23. # 人利用木棍攻击了狗,狗还剩50血