Saturday, August 22, 2020
Association in Java Definition and Examples
Relationship in Java Definition and Examples The affiliation relationship shows that a class thinks about, and holds a reference to, another class. Affiliations can be depicted as a has-a relationship in light of the fact that the normal execution in Java is using an example field. The relationship can be bi-directional with each class holding a reference to the next. Collection and sythesis are kinds of affiliation connections. Affiliations go along with at least one of one thing against at least one of something else. An educator may be related with a school course (a balanced relationship) yet in addition 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 basic war game with an AntiAircraftGun class and a Bomber class. The two classes should know about one another on the grounds that they are intended to demolish 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.