Python 类II

1.今日内容

  1. 从空间角度研究类
    1. 对象操作属性
    2. 类名操作属性
    3. 对象取值顺序
    4. 类名取值顺序
  2. 类与类之间的关系
    1. 依赖关系
    2. 组合关系

2.具体内容

  1. 从空间角度研究类

    • 对象操作属性

      class A:
        address = '绿草地'
        
        def __init__(self,name):
          self.name = name
          
        def func(self):
          if self.name = '小李':
            self.skins = '吉利服'
            
        def func1(self):
          print(self.__dic__)
          A.aaa = '小子'
          
      obj = A('小李')
      respons = input('姓名:')
      if respons == '消费':
        obj.weapon = 'AWM'
      print(obj.__dict__)#{'name': '小李', 'weapon': 'AWM'}
      print(A.__dict__)
      '''
      {'__module__': '__main__', 'address': '美丽富饶的沙河', '__init__': <function A.__init__ at 0x10ceac378>, 'func': <function A.func at 0x10ceac400>, 'func1': <function A.func1 at 0x10ceac840>, '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None}
      '''
      
      obj.func()
      print(obj.__dict__)#{'name': '小李', 'weapon': 'AWM', 'skins': '吉利服'}
      
      • Remark:被封装属性的对象,在内存中与类是两个独立的内存空间,因此,无论给对象封装什么属性,都不会改变原有类中的属性以及方法
    • 类名操作属性

      class A:
        address = '绿草地'
        
        def __init__(self,name):
          self.name = name
          
        def func(self):
          if self.name = '小李':
            self.skins = '吉利服'
            
        def func1(self):
          print(self.__dic__)
          A.aaa = '小子'
          
      A.temp = 40
      print(A.__dict__)
      
      obj = A('小李')
      A.func1(obj)
      print(A.__dict__)
      

      输出:

      {'__module__': '__main__', 'address': '美丽富饶的沙河', '__init__': <function A.__init__ at 0x10d677378>, 'func': <function A.func at 0x10d677400>, 'func1': <function A.func1 at 0x10d677840>, '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None, 'teamp': '39.5'}
      
      {'name': 'dsb'}
      
      {'__module__': '__main__', 'address': '美丽富饶的沙河', '__init__': <function A.__init__ at 0x10d677378>, 'func': <function A.func at 0x10d677400>, 'func1': <function A.func1 at 0x10d677840>, '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>, '__doc__': None, 'teamp': '39.5', 'aaa': '易水寒'}
      
      
    • 对象取值顺序

      #对象查询某个属性的顺序
      
      当前对象空间--> 类空间--> 父类空间-->
      
    • 类名取值顺序

      #类查询某个属性的顺序
      
      类空间--> 父类空间-->
      
    • Remark:

      • 单向不可逆
      • 对象与对象之间原则上相互独立(除去组合关系)
  2. 类与类的关系

    • 依赖关系

      • 依赖关系(主从关系):将一个类的类名或者对象作为实参传给另一个类的方法中
      class Elephant:
        
        def __init__(self,name):
          self.name = name
          
        def open(self,p1):
          print(f'{self.name}芝麻开门')
          p1.open_door()
          
        def close(self,p1):
          print(f'{self.name}芝麻关门')
          p1.close_door()
          
      class Refrigerator:
        
        def __init__(self,name):
          self.name = name
          
        def open_door(self):
          print(f'{self.name}冰箱门被打开了')
        
        def close_door(self):
          print(f'{self.name}冰箱门被关上了')
      
      e1 = Elephant('奇奇')
      r1 = Refrigerator('美岐')
      e1.open(r1)
      e1.close(r1)
      
      # 奇奇芝麻开门
      # 美岐冰箱门被打开了
      # 奇奇芝麻关门
      # 美岐冰箱门被关上了
      
    • 组合关系(关联组合聚合)

      • 组合关系:将一个类的对象封装到另一个类的属性中
      class Boy:
        
        def __init__(self,name):
          self.name = name
          
        def meet(self,GirlFriend = None):
          self.GirlFriend = GirlFriend
          
        def have_diner(self):
          if self.GirlFriend:
            print(f'{self.name}请{self.GirlFriend.name}一起吃饭')
            self.GirlFriend.shopping(self)
          else:
            print('单身狗啊,还是自己吃顿好的')
            
      class Girl:
        
        def __init__(self,name,age):
          self.name = name
          self.age = age
          
        def shopping(self,BoyFriend):
          print(f'{BoyFriend.name}{self.name}一起去购物')
          
      jack = Boy('杰克')
      jenny = Girl('詹尼',18)
      wu.meet()
      wu.have_diner()
      wu.meet(jenny)
      wu.have_diner()
      
      # 单身狗啊,还是自己吃顿好的
      # 杰克请詹尼一起吃饭
      # 杰克詹尼一起去购物
      
      • Remark:
        • 对象只能调用自身所在类所实例化的对象空间内的属性,不能查看其他类中的属性与调用其他类中的方法
        • 无论是组合关系还是依赖关系,为避免程序写死,在传参时候,始终传的是对象空间,即一个内存地址。只有当最后部分的需求时,才使用对象调用对象空间的属性
原文地址:https://www.cnblogs.com/xiaohei-chen/p/12070197.html