The relationship between Java classes

Java introduces the association , aggregation relationships , synthetic relationships and dependencies
Incidentally introduction is-a, is-like-a,has-a

Relations between classes

Generalization

Between classes and class inheritance relationship between the interfaces and interface inheritance relationship

Generalization, inheritance: class inheritance, interface inheritance

Realization relationship

Class implements the interface for

· Realize the relationship between class implements the interface

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)//获取朋友的地址
        }
    }
    

· Relationship through the "I" can find friends

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 overall relationship and aggregation relationship part: the declaration part of the cycle and the overall unrelated

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;
    }
    

· Synthetic relationship stronger than the aggregation relationship, part of the life cycle is determined by the overall

Dependencies

Dependency is weak, a weak association than one relationship

In the javalanguage 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{
    }
    

· Weaker than the association is to call off the relationship only

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;
    }
    
Published 34 original articles · won praise 30 · views 40000 +

Guess you like

Origin blog.csdn.net/wql2014302721/article/details/104658884