如何在Java中编写Composition和Aggregation [英] How to write Composition and Aggregation in java
问题描述
我想知道如何在java中识别组合和聚合代码。我有C ++代码,但我不明白如何在java中写。
I want to know how to identify composition and aggregation code in java. I have C++ Code, But I don't understand how to write in java.
组合
class A {};
class B { A composited_A; };
通过指针汇总
class A {};
class B
{
A* pointer_to_A;
B(A anA){
pointer_to_A = &anA;
}
任何人都可以告诉我如何在JAVA中工作。 (我知道什么是Composition和聚合)
};
Can anyone please tell me how both are working in JAVA. (I know what is meant by Composition and aggregation ) };
推荐答案
Java本身根本不区分组成和聚集之间。你不能在Java类型系统中表达引用的所有权的想法 - 如果你明确需要表达所有权,你必须用其他方法(通常只是通过构造一个新对象存储在类的实例中)来表示。
Java itself simply does not make the distinction between composition and aggregation. You cannot express the idea of ownership of a reference in the Java type system – if you explicitly need to express ownership you must denote this with some other means (usually simply by constructing a new object to be stored in the instance of a class).
因为Java有一个GC,内存的所有权不需要表示;内存只由GC拥有和管理。当然,对于其他资源也不是这样,在这里您可能仍然想表达所有权。
Since Java has a GC, ownership for memory doesn’t need to be expressed at all; memory is owned and managed solely by the GC. Of course, the same is not true for other resources and here you might still want to express ownership.
这篇关于如何在Java中编写Composition和Aggregation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!