python 22 with the object class

21. From the perspective of the space-based research

1.1 Add Object attributes:

    在类的__init__中可以添加;
    在类的方法中也可以添加;
    在类的内部也可以添加。

Add 1.2 class properties:

    在类的方法中可以添加,传参;
    在类的外部也可添加。

1.3 class relationship with the object:

类名(): 实例化对象,会在对象空间生成一个类对象指针,指向类空间。

对象空间有相同的名字时,肯定先从对象空间找。对象之所以可以找到类。是因为对象空间中有类对象指针。
对象查找属性的顺序:  对象.名字
    对象空间——>类空间——>父类空间
    
类查找属性的顺序:   类.名字
    类空间——>父类空间

2. Class and Class direct relationship

2.1 class relations and class:

  1. Dependencies
  2. connection relation
  3. Combination of relationship
  4. Aggregation relationship
  5. Realization relationship
  6. Inheritance (one of the three major categories of features: inheritance.)

2.2 dependence - from the main points

The class name of a class or object is passed to the method in another class.

class Elephant:
    
    def __init__(self,name):
        self.name = name

    def open(self, argv):
        print(f"{self.name} 默念三声 开门")
        argv.be_open()

    def close(self, argv):
        print(f"{self.name} 默念三声 关门")
        argv.be_close()
        
        
class Refrigerator:
    
    def __init__(self,name):
        self.name = name

    def be_open(self):
        print(f"{self.name} 被打开")

    def be_close(self):
        print(f'{self.name} 被关上')

meet = Elephant("MEET")
haier = Refrigerator("海尔")

meet.open(haier)   # 对象名做参数传入到类的方法中
meet.close(haier)

2.3 combination relationship

The object class attribute object encapsulated into another class. By using, in the method of changing the external encapsulates.

self: always-accepting default object of the present class or subclass.

class Boy:
    def __init__(self, name, girlfirend = None):
        self.name = name
        self.girlfirend = girlfirend
    def have_a_dinner(self):
        if self.girlfirend:
            print(f'{self.name}与他的{self.girlfirend.age}岁女朋友{self.girlfirend.name}一起烛光晚餐!')
        else:
            print("单身狗,吃什么吃!")
    def girl_skill(self):
        print(f'{self.name}的女朋友 ', end='')
        self.girlfirend.skill()

class Girl:
    def __init__(self,name,age):
        self.name = name
        self.age = age

    def skill(self):
        print(f'{self.name}的职业是老师。')

meet = Boy("宝元")
alex = Girl("alex", 20)
meet.girlfirend = alex   # 将类的对象封装到另一个类的对象的属性中
meet.have_a_dinner()
meet.girl_skill()
class GameRole:
    def __init__(self,name,ad,hp):
        self.name = name
        self.ad = ad
        self.hp = hp

    def attck(self,p1):
        p1.hp = p1.hp - self.ad
        print(f'{self.name}攻击{p1.name},{p1.name}掉了{self.ad}血,还剩{p1.hp}血')

    def equipment_wea(self,wea):
        self.weapon = wea     # Sword

class Weapon:   # 武器类

    def __init__(self,name,ad):
        self.name = name
        self.ad = ad

    def weapn_attack(self,p1,p2):
        p2.hp =p2.hp - self.ad
        print(f'{p1.name}利用{self.name}打了{p2.name}一下,{p2.name}掉了{self.ad}血,还剩{p2.hp}血. ')

gailun = GameRole("盖伦",10,100)
xin = GameRole("赵信",20,80)

Sword = Weapon("大宝剑",15)
Musket = Weapon("长枪",30)

gailun.equipment_wea(Sword) # 将Sword封装到gailun对象
gailun.weapon.weapn_attack(gailun, xin) 
# 通过英雄人物控制武器执行攻击动作

xin.equipment_wea(Musket)
xin.weapon.weapn_attack(xin,gailun)

Dependence and combination class and class generating relationship so, to enhance coupling.

Guess you like

Origin www.cnblogs.com/yzm1017/p/11313573.html