如何在Java中编写Composition和Aggregation

编程入门 行业动态 更新时间:2024-10-25 16:19:23
本文介绍了如何在Java中编写Composition和Aggregation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道如何在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

本文发布于:2023-11-22 20:14:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618850.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Java   Aggregation   Composition

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!