Generalization (generalization) Association (association) Dependency (Dependency) aggregation (Aggregation)

Source: Internet
Author: User
Tags inheritance
The relationship between classes and classes plays an important role in understanding object-oriented, and is often asked in interviews before, and I'll introduce you here. The following relationship exists between classes and classes: (1) Generalization (generalization) (2) Association (Association) (3) dependent (Dependency) (4) Aggregation (Aggregation)

UML Diagram and application code example: 1. Generalization (generalization) [generalization] New Paradise represents the inheritance relationship between a class and a class, and the inheritance between interfaces and interfaces. Or a class-to-interface implementation relationship. A generalized relationship is from a subclass to a parent class, as opposed to an inherited or implemented method. [Concrete] Parent Class parent class instance =new subclass () [UML diagram] (Figure 1.1) Diagram 1.1 Animal class and Tiger class, The dependency of the dog class [code performance] class  animal{}    class  tiger extends animal{}    public   Class test    {       public  Void test ()        {            animal a=new tiger ();        }    }  

2. Dependency (Dependency) [dependent] for two relatively independent objects, when an object is responsible for constructing an instance of another object, or a service that relies on another object, the two objects are primarily dependencies. [Specific performance] dependencies are represented in local variables, method parameters, and calls to static methods [real-world examples] for example, if you are going to screw a screw, do you want to use (i.e., rely on) a screwdriver (screwdriver) to help you complete the screw ( Screw) Work [UML performance] (Figure 1.2)

Figure 1.2 The dependency of the person class and the screwdriver class [code performance] public class person{ /** screws */ public void screw (screwdriver screwdriver) { screwdriver.screw ();     }    }  

. Association (Association)[related]For two relatively independent objects, when an instance of one object has a fixed correspondence with some specific instance of another object, the two objects are associated.[Specific performance]Association relationships are implemented using instance variables.[Example of reality]Like whatCustomerand orders, each order corresponds to a specific customer, each customer corresponds to some specific order, and then, for example, a company and an employee, each company corresponds to a specific employee, each employee corresponds to a specific company[UML diagram] (Figure 1.3) Figure 1.3 the relationship between company and employee [Code Performance] Publicclass company{ private employee employee; Public Employee GetEmployee () { return employee;     }    Public void Setemployee (Employee employee) { This.employee=employee;     }    //Company Operation Public void Run () { employee.startworking ();     }    }  

(4) polymerization (Aggregation) [Aggregation]When object A is added to object B and becomes part of Object B, the aggregation relationship is between object B and object A. Aggregation is one kind of correlation relation, it is strong association relation, emphasizing the relation between whole and part. New[Specific performance]As with associative relationships, aggregation relationships are also implemented by instance variables. There is no way to differentiate the relationship and the aggregation relationship, and the difference between the two can be better differentiated by semantics.[Differences between associations and aggregations](1) The two objects involved in the association relationship are at the same level. For example, people and bicycles are an association, not an aggregation, because people are not made up of bicycles. The two objects involved in the aggregation relationship are at an unequal level, one representing the whole, and the other representing the part. For example, the computer and its monitor, keyboard, motherboard and memory are aggregation relationships, because the motherboard is a part of the computer. (2) for two objects with aggregation relationships, especially strong aggregation relationships, the overall object restricts the life cycle of its constituent objects. The object of a partial class cannot exist alone, its life cycle depends on the life cycle of the object of the whole class, and when the whole disappears, the part disappears. For example, Zhang San's computer is stolen, then all the components of the computer are not there, unless Zhang San in advance to remove some of the computer components (such as hard disk and memory).[UML diagram] (Figure 1.4) Figure 1.3 Aggregation relationship of computers and Components [code performance] Publicclass computer{ private CPU CPU; Public CPU getcpu () { return CPU;     }    Public void SetCPU (CPU CPU) { this.cpu=cpu;     }    //Turn on the computer Public void Start () { //CPU Operation Cpu.run ();     }   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.