Saturday, August 22, 2020
Association in Java Definition and Examples
Relationship in Java Definition and Examples The affiliation relationship demonstrates that a class thinks about, and holds a reference to, another class. Affiliations can be portrayed as a has-a relationship on the grounds that the regular execution in Java is using an example field. The relationship can be bi-directional with each class holding a reference to the next. Accumulation and creation are sorts of affiliation connections. Affiliations go along with at least one of one thing against at least one of something else. A teacher may be related with a school course (a balanced relationship) yet additionally with every understudy in her group (a one-to-numerous relationship). The understudies in a single area may be related with the understudies in another segment of a similar course (a many-to-numerous relationship) while all the segments of the course identify with a solitary course (a many-to-one relationship). Affiliation Example Envision a straightforward war game with an AntiAircraftGun class and a Bomber class. The two classes should know about one another in light of the fact that they are intended to devastate one another: open class AntiAirCraftGun { à â private Bomber target; à â private int positionX; à â private int positionY; à â private int harm; à â public void setTarget(Bomber newTarget) à â { à â â â this.target newTarget; à â } à â //rest of AntiAircraftGun class } open class Bomber { à â private AntiAirCraftGun target; à â private int positionX; à â private int positionY; à â private int harm; à â public void setTarget(AntiAirCraftGun newTarget) à â { à â â â this.target newTarget; à â } à â //rest of Bomber class } The AntiAirCraftGun class has-a Bomber object and the Bomber class has-an AntiAirCraftGun object.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.