gpt4 book ai didi

java - ArrayList 是聚合还是组合?

转载 作者:太空宇宙 更新时间:2023-11-04 09:49:17 24 4
gpt4 key购买 nike

我正在准备编程考试,遇到了这个问题,我知道在聚合中对象是借用的,而在组合中对象是拥有的。答案是作文吗?

ArrayList<X> X 的聚合还是 X 的组合?

ArrayList<Point> pts = new ArrayList<Point>();
Point p = new Point(0., 0., 0.);
pts.add(p);
p.setX( 10.0 );
System.out.println(p);
System.out.println(pts.get(0));

最佳答案

来自here :

Simple rules:

  1. A "owns" B = Composition : B has no meaning or purpose in the system without A
  2. A "uses" B = Aggregation : B exists independently (conceptually) from A

因此,这实际上取决于您的型号。列表中的元素是否可以在没有列表的情况下存在。元素是否必须放入列表中才有意义?

ArrayList<Point>为例,我认为这是一个聚合。

关于java - ArrayList<X> 是聚合还是组合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54988783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com