There are roughly 5 types of relationships between classes:
1, dependency relationship (Dependency)
One-way, which indicates that one class relies on the definition of another class, where the change of one class affects the other class, and is a "use a" relationship
If a depends on B, then B behaves as a local variable, method parameter, static method call, etc.
public class Person {public
void dosomething () {
card = new Card ();//local variable
....
}
}
public class Person {public
void DoSomething (card) {//method parameter
....
}
}
public class Person {public
void dosomething () {
int id = card.getid ();//static method call
...
}
}
2, association Relationship (Association)
One-way or two-way (usually we need to avoid the use of bidirectional correlation), is a "has a" relationship, if a one-way association B, it can be said a has a B, usually represented as a global variable
public class Person {public
phone phone;
public void Setphone (phone phone) {
this.phone = phone;
}
Public Phone Getphone () {
return phone;
}
}
3, aggregation relationship (Aggregation)
One-way, the correlation relationship between one, and the relationship between the difference is semantically, the associated two objects are usually equal, aggregation is generally unequal, there is a sense of the whole and local, the implementation of the difference is not small
Class consists of student, its life cycle is different, the whole does not exist, some still exist, the team is now dissolved, people are still, you can join other groups
public class Team {public person person
;
Public Team {
This.person = person;
}
}
4, combinatorial relationship (composition)
One-way, a special aggregation relationship with strong dependence
The Head,body,arm and leg groups synthesize people, whose life cycle is the same, and if the whole is absent, the part will perish.
public class Person {public
head head;
public body Body;
public arm arm;
Public Leg Leg;
Public person () {
head = new head ();
BODY = new Body ();
ARM = new Arm ();
Leg = new leg ();
}
}
5, inheritance Relationship (inheritance)
class implements an interface, a class inherits an abstract class, and a class inherits a parent class that belongs to this relationship
Can be divided into finer points:
Implementation (REALIZATION): Class implementation interface belongs to this relationship
Generalization (generalization): "is a" relationship, class inherits abstract class, class inherits parent class belongs to this kind of relationship