Relationships between objects - python_ Object Oriented

1. The associated class object

class the Person:
     DEF  the __init__ (Self, name, Age, Sex): 
        the self.name = name 
        self.age = Age 
        self.sex = Sex 
        self.parter = None     # first blank, behind the assignment (for the associated objects) 
    DEF do_private_stuff (Self):
         Pass 

p1 = the Person ( ' WDC ' , 22, ' male ' ) 
P2 = the Person ( ' YHF ' , 23, ' female ' ) 

# two-way binding to associate objects
p2 = p1.parter # Copies objects to the object p1, p2 parter property 
p2.parter p1 = # Copy an object to the object p1 and p2 parter property 
# print object parter Properties to see its contents. 
Print (p1.parter.name, p2.parter.name)

2. The relationship of objects in contact with the class

class the Person:
     DEF  the __init__ (Self, name, Age, Sex): 
        the self.name = name 
        self.age = Age 
        self.sex = Sex 
        self.parter = None     # first blank, behind the assignment (for the associated objects) 
    DEF do_private_stuff (Self):
         Pass 

p1 = the Person ( ' WDC ' , 22, ' male ' ) 
P2 = the Person ( ' YHF ' , 23, ' female ' ) 

# two-way binding to associate objects
p2 = p1.parter # Copies objects to the object p1, p2 parter property 
p2.parter p1 = # Copy an object to the object p1 and p2 parter property 
# print object parter Properties to see its contents. 
Print (p1.parter.name, p2.parter.name) 

# bidirectional contacting relationship 
p2.parter = None 
p1.parter = None 

Print (p1.parter, p2.parter)

 

Guess you like

Origin www.cnblogs.com/wangdianchao/p/11837113.html