There are roughly 5 relationships between classes:
1, Dependencies (Dependency)
One-way, which means that a class relies on the definition of another class, where a change in one class affects another class, 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 Card
= new card ();//local variable
...
}
}
public class Person {public
void DoSomething (Card card) {//Method parameters
...
}
}
public class Person {public
void dosomething () {
int id = card.getid ();//static method call
...
}
}
2, association Relationship (Association)
One-way or bidirectional (usually we need to avoid using bidirectional association relations), is a "has a" relationship, if a one-way association B, you can say 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, polymerization relationship (Aggregation)
One-way, the relationship between the one, and the relationship between the difference is semantically, the association of two objects are usually equal, aggregation is generally unequal, there is a whole and local sense, the realization of the difference is not
Class consists of student, its life cycle is different, the whole does not exist, some still exist, the current team dissolved, people still, you can join other groups
public class Team {public person person
;
Public team (person) {
This.person = person;
}
}
4, combination relationship (composition)
One-way, is a strong dependence of a special aggregation relationship
The Head,body,arm and leg groups synthesize people, and their life cycle is the same, and if the whole does not exist, parts will die out.
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 the parent class, which belongs to this relationship
Can be divided more carefully:
Implementation (REALIZATION): Class implementation interface belongs to this relationship
Generalization (generalization): The "is a" relationship, the class inherits the abstract class, the class inherits the parent class all belong to this relationship