我读到这样的说法:“当对象具有自己的生命周期并且子对象只能与一个父对象关联时,就会发生聚合”。但是,它对我的代码来说很好用:-
class Country:
def __init__(self,name=None,population=0):
self.name = name
self.population = population
def printDetails(self):
print("Country Name:",self.name)
print("Country Population",self.population)
class Person:
def __init__(self,name,country):
self.name = name
self.country = country
def printDetails(self):
print("Person Name:",self.name)
self.country.printDetails()
class Man:
def __init__(self,self.name)
self.country.printDetails()
c = Country("Wales",1500)
p = Person("Joe",c)
m = Man('John',c);
p.printDetails()
m.printDetails()
c.printDetails()