Python -- object oriented: class space problems and the relationship between classes

Keywords: PHP

7.2.1 space problems

Where can I add object properties? -- object properties can be added not only in init, but also outside other methods or classes of a class

Where can I add static properties of a class? -- properties of a class can be added not only inside the class, but also outside the class.

Object how to find the properties of a class. Illustration

Order of object search attributes: first find from object space -- > class space -- > parent class space -- >

Order of class name search attributes: first search from this class space -- > parent class space -- >

The above order is unidirectional and irreversible. The class name cannot find the property of the object.

7.2.2 dependency

# For example, dependency: it is used to pass the object or class name of one class to the method of another class.
class Elephant:
    def __init__(self, name):
        self.name = name

    def open(self,ref1):
        print(f'Elephant{self.name}Meditation:Open the refrigerator door')
        ref1.open_door()

    def close(self,ref2):
        print(f'Elephant{self.name}Meditation:Close the refrigerator door')
        ref2.close_door()

class Refrigerator:

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

    def open_door(self):
        print(f"{self.name}The refrigerator has been opened")

    def close_door(self):
        print(f"{self.name}The refrigerator is closed")

el = Elephant('Xiao Bai')
ref = Refrigerator('AUX')
el.open(ref)
el.close(ref)

# Execute the method of the Elephant class to implement two procedures (1. Open the refrigerator door 2. The refrigerator door is opened)

7.2.3 combination relationship

# Composition: encapsulate the objects of one class into the attributes of the objects of another class.
# Example 1 a boyfriend is related to a girlfriend. A girlfriend is related to a boyfriend. This relationship can be mutual or single.
class Boy:
    def __init__(self,name):
        self.name = name

    def meet(self,girl_friend=None):
        self.girl_friend = girl_friend     # self.girl_friend = Girl's object space

    def have_dinner(self):
        if self.girl_friend:
            print(f'{self.name}please{self.girl_friend.name}Eat together')
            self.girl_friend.shopping(self)  # self = Boy object space
        else:
            print('Eat dog food.')

class Girl:
    def __init__(self,name,age):
        self.name = name
        self.age = age
    def shopping(self,boy):  # Boy = object space of boy
        print(f'{self.name}and{boy.name}Go shopping together')

wu = Boy('Wu Chao')
G = Girl('Flowery',48)
wu.meet(G)       # Execute the meet method in the Boy class and pass the class Girl as a parameter to girl_friend
wu.have_dinner()


# In example 2, a game character class is designed to instantiate several objects so that these game characters can fight each other.
class GameRole:
    def __init__(self,name,ad,hp):
        self.name = name
        self.ad = ad
        self.hp = hp

    # def attack(self,p1):
    #     p1.hp = p1.hp - self.ad
    #     print(f'{self.name} attacks {p1.name},{p1.name} loses {self.ad} blood, leaving {p1.hp} blood')

    def equit_weapon(self,wea):
        self.weapon = wea      # self.weapon = Weapon object space where composition is realized

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

    def weapon_attack(self,p1,p2):  # p1 = gailun  p2 = zhaoxin
        p2.hp -= self.ad
        print(f'{p1.name}utilize{self.name}Yes.{p2.name}Once,{p2.name}Left{p2.hp}blood')

gailun = GameRole('Galen', 10, 100)
zhaoxin = GameRole('Zhao Xin', 20, 90)
great_sword = Weapon('Big health care', 30)
gailun.equit_weapon(great_sword)   # Dependency: pass weapon as a parameter to the wea of the equit? Weapon method in GameRole
gailun.weapon.weapon_attack(gailun, zhaoxin)

Posted by rajmohan on Fri, 01 Nov 2019 19:39:48 -0700