aggregation java,如何在Java中编写Composition和Aggregation

在Java中,没有像C++那样明确区分组合(Composition)和聚合(Aggregation)。Java的垃圾回收机制使得内存所有权无需显式表达。然而,对于其他资源的管理,可能仍需表明所有权。组合和聚合主要通过类的实例成员来体现,组合是强拥有关系,聚合则是弱拥有关系。在Java中,通常通过构造函数传递对象引用来实现这两种关系。
摘要由CSDN通过智能技术生成

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.

Composition

class A {};

class B { A composited_A; };

Aggregation via Pointer

class A {};

class B

{

A* pointer_to_A;

B(A anA){

pointer_to_A = &anA;

}

Can anyone please tell me how both are working in JAVA. (I know what is meant by Composition and aggregation )

};

解决方案

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).

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值