关联关系多重性java实现_如何在Java中实现关联

关联关系多重性java实现

The association relationship indicates that a class knows about, and holds a reference to, another class. Associations can be described as a "has-a" relationship because the typical implementation in Java is through the use of an instance field. The relationship can be bi-directional with each class holding a reference to the other. Aggregation and composition are types of association relationships.

关联关系表示一个知道另一个类,并持有对该的引用。 关联可以描述为“具有”关系,因为Java中的典型实现是通过使用实例字段。 该关系可以是双向的,每个类都拥有对另一个类的引用。 聚集和组合是关联关系的类型。

Associations join one or more of one thing against one or more of another thing. A professor might be associated with a college course (a one-to-one relationship) but also with each student in her class (a one-to-many relationship). The students in one section might be associated with the students in another section of the same course (a many-to-many relationship) while all the sections of the course relate to a single course (a many-to-one relationship).

联想将一件事中的一个或多个与另一件事中的一个或多个连接在一起。 教授可能与大学课程相关(一对一关系),但也与班级中的每个学生相关(一对多关系)。 一个部分中的学生可能与同一课程的另一部分中的学生相关联(多对多关系),而该课程的所有部分都与单个课程相关(多对一关系)。

关联示例 ( Association Example )

Imagine a simple war game with an AntiAircraftGun class and a Bomber class. Both classes need to be aware of each other because they are designed to destroy each other:

想象一下一个具有AntiAircraftGun类和Bomber类的简单战争游戏。 这两个类需要彼此了解,因为它们被设计为互相破坏:

 public class AntiAirCraftGun {
  private Bomber target;
  private int positionX;
  private int positionY;
  private int damage;
  public void setTarget(Bomber newTarget)
  {
    this.target = newTarget;
  }
  //rest of AntiAircraftGun class
}
public class Bomber {
  private AntiAirCraftGun target;
  private int positionX;
  private int positionY;
  private int damage;
  public void setTarget(AntiAirCraftGun newTarget)
  {
    this.target = newTarget;
  }
  //rest of Bomber class
}

The AntiAirCraftGun class has-a Bomber object and the Bomber class has-a AntiAirCraftGun object.

AntiAirCraftGun类具有 -Bomber对象,而Bomber类具有 -AntiAirCraftGun对象。

翻译自: https://www.thoughtco.com/association-2034002

关联关系多重性java实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值