将对象列表提供给另一个 class ,而不是使这种关系聚合?
Would giving a list of objects to another class , not make this relation aggregation?
class A:
def __init__(self, id, name ,age):
self.id = id
self.name = name
self.age = age
def get_id(self):
return self.id
def get_name(self):
return self.name
def get_age(self):
return self.age
class B:
def __init__(self, lst, file):
self.lst = lst
self.file = file
def writefile(self):
fileObj = open(self.file, 'w')
write_string = ''
for person in self.lst:
for func in [person.get_id, person.get_name, person.get_age]:
write_string += func() + '\t'
write_string = write_string[:-1] + '\n'
fileObj.write(write_string[:-1])
fileObj.close()
def main():
file = 'sample.txt'
def loadfile():
try:
filez = open(file, 'r')
except FileNotFoundError:
filez = open(file, 'w')
filez.close()
filez = open(file, 'r')
lst = []
for line in filez:
id, name, age = line.split('\t')
age = date.rstrip('\n')
lst.append(A(id, name, age))
return lst
population = loadfile()
call = B(population, file)
def add():
obj_A1 = A('1','bilal','29')
obj_A3 = A('3','asda','54')
population.append(obj_A1)
population.append(obj_A3)
add()
def display():
for i in population:
if i.get_id() == '3':
print('found')
display()
call.writefile()
main()
我是 OOP 新手。我试图了解如果将对象的 列表 提供给 class B 是否会对它的关系产生任何影响。我希望这里的关系是 Class A 和 class B 之间的聚合,如果我没有错,因为我将 人口列表 添加到 class B ? ps。抱歉代码太长了。
根据 Martin Fowler 的 UML Distilled:
Aggregation is strictly meaningless; as a result, I recommend that you ignore it in your own diagrams.
这给我们留下了一个问题,即 lst
中包含的对象是否与 关联 或 组合 关系=14=]?
Composition 意味着正在组合的对象不会超过其容器 classes。这也意味着它们不被其他容器共享 classes.
关联 定义了一种比组合更松散的耦合关系,即一个对象仅引用另一个对象。在 python 中,这基本上转化为 class 具有特定实例属性的想法。
在您的代码中,您首先实例化 population
,然后将其传递给 B
的构造函数。假设你有另一个对象 C
可以接受一个列表对象,就像 B
:
class C:
def __init__(self, lst, file):
self.lst = lst
self.file = file
可以想象,稍后在您的 main
块中,可以重新使用 population
变量来实例化 C
。这将导致 B
和 C
之间共享实例属性。此外,如果您稍后在代码中的某处调用 del B
,它也不会破坏 population
。因此,您在这里只有 关联 关系。
如果您想实现真正的组合(我假设这就是聚合的意思),您可以在构造函数中实例化列表:
class B:
def __init__(self, file):
self.lst = loadfile(file)
self.file = file
或者您可以在 B
实例化时实例化列表,如下所示:
call = B(loadfile(file), file)
class A:
def __init__(self, id, name ,age):
self.id = id
self.name = name
self.age = age
def get_id(self):
return self.id
def get_name(self):
return self.name
def get_age(self):
return self.age
class B:
def __init__(self, lst, file):
self.lst = lst
self.file = file
def writefile(self):
fileObj = open(self.file, 'w')
write_string = ''
for person in self.lst:
for func in [person.get_id, person.get_name, person.get_age]:
write_string += func() + '\t'
write_string = write_string[:-1] + '\n'
fileObj.write(write_string[:-1])
fileObj.close()
def main():
file = 'sample.txt'
def loadfile():
try:
filez = open(file, 'r')
except FileNotFoundError:
filez = open(file, 'w')
filez.close()
filez = open(file, 'r')
lst = []
for line in filez:
id, name, age = line.split('\t')
age = date.rstrip('\n')
lst.append(A(id, name, age))
return lst
population = loadfile()
call = B(population, file)
def add():
obj_A1 = A('1','bilal','29')
obj_A3 = A('3','asda','54')
population.append(obj_A1)
population.append(obj_A3)
add()
def display():
for i in population:
if i.get_id() == '3':
print('found')
display()
call.writefile()
main()
我是 OOP 新手。我试图了解如果将对象的 列表 提供给 class B 是否会对它的关系产生任何影响。我希望这里的关系是 Class A 和 class B 之间的聚合,如果我没有错,因为我将 人口列表 添加到 class B ? ps。抱歉代码太长了。
根据 Martin Fowler 的 UML Distilled:
Aggregation is strictly meaningless; as a result, I recommend that you ignore it in your own diagrams.
这给我们留下了一个问题,即 lst
中包含的对象是否与 关联 或 组合 关系=14=]?
Composition 意味着正在组合的对象不会超过其容器 classes。这也意味着它们不被其他容器共享 classes.
关联 定义了一种比组合更松散的耦合关系,即一个对象仅引用另一个对象。在 python 中,这基本上转化为 class 具有特定实例属性的想法。
在您的代码中,您首先实例化 population
,然后将其传递给 B
的构造函数。假设你有另一个对象 C
可以接受一个列表对象,就像 B
:
class C:
def __init__(self, lst, file):
self.lst = lst
self.file = file
可以想象,稍后在您的 main
块中,可以重新使用 population
变量来实例化 C
。这将导致 B
和 C
之间共享实例属性。此外,如果您稍后在代码中的某处调用 del B
,它也不会破坏 population
。因此,您在这里只有 关联 关系。
如果您想实现真正的组合(我假设这就是聚合的意思),您可以在构造函数中实例化列表:
class B:
def __init__(self, file):
self.lst = loadfile(file)
self.file = file
或者您可以在 B
实例化时实例化列表,如下所示:
call = B(loadfile(file), file)