小编Sid*_*gga的帖子

Python中的聚合(OOP概念)是否限制子对象被其他对象拥有?

我读到这样一句话:“当对象有自己的生命周期并且子对象只能与一个父对象关联时,就会发生聚合”。但是,它对我的​​代码运行良好:-

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, name, country):
        self.name = name
        self.country = country

    def printDetails(self):
        print("Person Name:", self.name)
        self.country.printDetails()



c = Country("Wales", 1500)
p = Person("Joe", c)
m = Man('John', c);
p.printDetails()
m.printDetails()

c.printDetails()
Run Code Online (Sandbox Code Playgroud)

python oop class aggregation python-3.x

0
推荐指数
1
解决办法
75
查看次数

标签 统计

aggregation ×1

class ×1

oop ×1

python ×1

python-3.x ×1