Java introduces the association , aggregation relationships , synthetic relationships and dependencies
Incidentally introductionis-a
,is-like-a
,has-a
Relations between classes
Generalization
Between classes and class inheritance relationship between the interfaces and interface inheritance relationship
Realization relationship
Class implements the interface for
connection relation
The connection between the class and class, a class can know another class properties and methods used in the java language member variables embodied
Me me = new Me(new Friend())
// build relationships
-
For example, the relationship between me and my friends
- I can find my friend's address
public class Friend{ public String addr; } class Me{ Friend f; public Me(Friend f){ this.f = f; } // test public static void main(String[] args){ Friend f = new Friend(); Me me = new Me(f); System.out.println(me.f.addr)//获取朋友的地址 } }
Aggregation relationship
Aggregation relationship is a relationship is a strong relationship , and is an integral part of the relationship. For example: car and tires
Relationship and associated with a different class association relationship on an equal level, and the relationship between the polymerization type and the like at different levels on behalf of a whole, a part of the representative
Aggregation relationship is the relationship between parts and the whole
Overall not rely on section
Nor on the part of the whole
Overall not decide lifecycle parts (not part of the relationship between humans and limbs aggregation relationship)
-
For example, the relationship between the classroom and students
public class Student{ } public class ClassRoom{ List<Student> s; }
THE RELATIONSHIP
Synthesis of relationship is also associated with a relationship, stronger than the aggregation relationship
And synthetic polymeric relationship relations are similar, except that:
RELATIONSHIP BETWEEN whole and the part are closely linked , the overall life cycle decision part of the life cycle
-
For example, the relationship between humans and the limbs
public class human{ List<四肢> s; }
Dependencies
Dependency is weak, a weak association than one relationship
In the
java
language reflected in: call return values, parameters, local variables and static methods
-
E.g
public class Test{ public void m1(){ User user = new User();//依赖关系 } } class user{ }
is-a
/is-like-a
/has-a
is-a
-
Dog is a Animal
-
It is-a relationship is inherited
public class Animal{ } class Dog extends Animal{ }
is-like-a
-
A is like a I
-
is-like-a relationship is to achieve
public Interface I{ void m1(); } class A implements I { public void m1(){ } }
has-a
-
A has a B
-
has-a relationship comprising
public class B{ } class A{// A has a B private B b; }