Object-oriented relationship between class and class space issues

1. class space issues

1.1 Where can add object properties

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

    def func(self,sex):
        self.sex = sex
# 类外面可以:
obj = A('barry')
obj.age = 18
print(obj.__dict__)  # {'name': 'barry', 'age': 18}

# 类内部也可以:
obj = A('barry') # __init__方法可以。
obj.func('男')  # func 方法也可以。

** Summary: attributes of the objects which can not only __init__ added, may be added to the outside of the other class or classes of methods. **

1.2 Where can add static properties of the class

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

    def func(self,sex):
        self.sex = sex
    
    def func1(self):
        A.bbb = 'ccc'
# 类的外部可以添加

A.aaa = 'taibai'
print(A.__dict__)

# 类的内部也可以添加。

A.func1(111)
print(A.__dict__)

Summary: The properties of the class not only within the class is added, can also be added outside the class.

1.3 How to find the object properties of the class

Find the object attribute order: first from object space to find ------> class space to find ------> ------- parent find space> .....

Find the class name attribute order: start looking for this type of space -------> -------- find the parent class space> ........

The above sequence is irreversible way, the class name can not find the object's properties.

2. The relationship between class and class

Zoomed thousands of the world, there are rules and laws among all our classes and objects are all things in the zoomed thousands of the world into ⾏ classification. There is a corresponding relationship between those things. Equally between classes .. so the world ⾯ to object and class relations following classes:

  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.1 Dependencies

Therefore especially First, we designed has a scene. That was the first example of submenus. Zoomed should dress like a refrigerator. Note In this scenario, the fact is there is two things. ⼀ is a zoomed image, zoomed as responsible for the entire event control of them, there is ⼀ a refrigerator, the refrigerator was responsible zoomed image manipulation.

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

    def open(self,obj1):
        '''
        开⻔
        '''
        print('大象要开门了,默念三声,开')
        obj1.open_door()

    def close(self):
        '''
        关⻔
        '''
        print('大象要关门了,默念三声,关')


class Refrigerator:

    def open_door(self):
        print("冰箱⻔被打开了")

    def close_door(self):
        print("冰箱⻔被关上了")


elphant1 = Elphant('大象')
haier = Refrigerator()
elphant1.open(haier)

  The main action is initiated by the elephant, you have to close this to practice. Dependencies: The method of the object class or a class name passed to another class of use. At this point, we say, it is dependent on the relationship between the zoomed image and refrigerator. Use me of you. But you do not belong to me. This relationship is the weakest, such as. Between the company and employees. For official member ⼯, it must sign labor contracts. CAUTION had to wait on. but if it is part-time. that ⽆ called. you need to come up No you can worship. individual cases from part-time (temporary ⼯) belongs dependencies. I Use you but you do not belong to me

2.2 combination relationship

  1. Combining relationship. ⼀ belonging to the species relationships special case almost the writing. Relations even tighter than polymeric compositions, such as Face of zoomed brain, Center Weighted dirty various organs. These organs combined into ⼀ Face this time. If the hanging Face the other East ⻄ also followed hung up
class Boy:
    def __init__(self,name,girlFriend=None):
        self.name = name
        self.girlFriend = girlFriend

    def have_a_diner(self):
        if self.girlFriend:
            print('%s 和 %s 一起晚饭'%(self.name,self.girlFriend.name))
        else:
            print('单身狗,吃什么饭')


class Girl:
    def __init__(self,name):
        self.name = name
b = Boy('日天')
b.have_a_diner() # 此时是单身狗

# 突然有一天,日天牛逼了
b.girlFriend = '如花'
b.have_a_diner()  #共进晚餐
# wusir 生下来就有女朋友 服不服
gg = Girl('小花')
bb = Boy('wusir', gg)
bb.have_a_diner()

# 结果嫌他有点娘,不硬,分了
bb.girlFriend = None
bb.have_a_diner()

Note that this time is the relationship between the two classes Boy and Girl. Two classes of objects closely practicing which ⼀ a no. For another time on a very, very lonely. Relationship, in fact, I need you. You I also belong. this is the relationship. this relationship has, like many, many, such as the relationship between the school and the teacher.

# 老师属于学校,必须有学校才可以工作
class School:

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


class Teacher:

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

s1 = School('北京校区','美丽的沙河')
s2 = School('上海校区','上海迪士尼旁边')
s3 = School('深圳校区','南山区')

t1 = Teacher('武大',s1)
t2 = Teacher('海峰',s2)
t3 = Teacher('日天',s3)

print(t1.school.name)
print(t2.school.name)
print(t3.school.name)

But the school also depends on the teacher, so the teacher school should depend on each other.

class School:

    def __init__(self,name,address):
        self.name = name
        self.address = address
        self.teacher_list = []

    def append_teacher(self,teacher):
        self.teacher_list.append(teacher)
        
class Teacher:

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

s1 = School('北京校区','美丽的沙河')
s2 = School('上海校区','上海迪士尼旁边')
s3 = School('深圳校区','南山区')

t1 = Teacher('武大',s1)
t2 = Teacher('海峰',s2)
t3 = Teacher('日天',s3)

s1.append_teacher(t1)
s1.append_teacher(t2)
s1.append_teacher(t3)

# print(s1.teacher_list)
# for teacher in s1.teacher_list:
#     print(teacher.name)

Okay. This is the relationship. When we appeared on the logic. I need you. You have to belong to me. This is a logical relationship. That attention. Closeness of this relationship to be tighter than dependence on the screen for and more. Why? think about it

As for the relationship between the polymerization and combination relation, in fact, not very different code, we will go to a combination of Examples:

Combination: a class object is encapsulated into another attribute of the object class, called combination.

Let's design a game character class, make a few instances of these objects allow game characters to achieve melee results.

class Gamerole:
    def __init__(self,name,ad,hp):
        self.name = name
        self.ad = ad
        self.hp = hp
    def attack(self,p1):
        p1.hp -= self.ad
        print('%s攻击%s,%s掉了%s血,还剩%s血'%(self.name,p1.name,p1.name,self.ad,p1.hp))
gailun = Gamerole('盖伦',10,200)
yasuo= Gamerole('亚索',50,80)

#盖伦攻击亚索
gailun.attack(yasuo)
# 亚索攻击盖伦
yasuo.attack(盖伦)

But this does not mean to attack each other, the general war game like manner to the aid of weapons, weapon is a class, object class contains a lot of weapons: a sword stick sword ax hook fork and so on, so let's write a weapon class.

class Gamerole:    
    def __init__(self,name,ad,hp):        
        self.name = name        
        self.ad = ad        
        self.hp = hp    
    def attack(self,p1):        
        p1.hp -= self.ad        
        print('%s攻击%s,%s掉了%s血,还剩%s血'%(self.name,p1.name,p1.name,self.ad,p1.hp))
class Weapon:    
    def __init__(self,name,ad):        
        self.name = name        
        self.ad = ad    
    def weapon_attack(self,p1,p2):        
        p2.hp = p2.hp - self.ad - p1.ad        
        print('%s 利用 %s 攻击了%s,%s还剩%s血' %(p1.name,self.name,p2.name,p2.name,p2.hp))

Next With weapons to attack each other:

pillow = Weapon('绣花枕头',2)
pillow.weapon_attack(barry,panky)
# 但是上面这么做不好,利用武器攻击也是人类是动作的发起者,所以不能是pillow武器对象,而是人类利用武器攻击对方

So, modify the code.

class Gamerole:
    def __init__(self,name,ad,hp):
        self.name = name
        self.ad = ad
        self.hp = hp
    def attack(self,p1):
        p1.hp -= self.ad
        print('%s攻击%s,%s掉了%s血,还剩%s血'%(self.name,p1.name,p1.name,self.ad,p1.hp))
        
    def equip_weapon(self,wea):
        self.wea = wea  # 组合:给一个对象封装一个属性改属性是另一个类的对象
class Weapon:
    def __init__(self,name,ad):
        self.name = name
        self.ad = ad
    def weapon_attack(self,p1,p2):
        p2.hp = p2.hp - self.ad - p1.ad
        print('%s 利用 %s 攻击了%s,%s还剩%s血'
              %(p1.name,self.name,p2.name,p2.name,p2.hp))


# 实例化三个人物对象:
barry = Gamerole('太白',10,200)
panky = Gamerole('金莲',20,50)
pillow = Weapon('绣花枕头',2)

# 给人物装备武器对象。
barry.equip_weapon(pillow)

# 开始攻击
barry.wea.weapon_attack(barry,panky)

Is a combination of the above, as long as the person .equip_weapon this method, then the characters on the object encapsulates a weapon, and then use their weapons object to call weapon_attack method class.

Guess you like

Origin www.cnblogs.com/fengqiang626/p/11304238.html