UML Learning: Association relationships (aggregations, combinations) of class diagram classes

Source: Internet
Author: User

The Concept of association

An association is used to represent a structural relationship between objects of two or more classes, which is represented in code as a class that contains the application of one or more objects to another class as a property.

Program Demo: Association relationship (Code/assocation)

Suppose: A company has only three employees: an employee either has no job or can only work in a company. (Code is simplified, the main description of the association relationship)

Person class:

Class person
    {public
        string name;
        Public person (string ename)
        {
            name = ename;
        }
        public void Startwork ()
        {
            Console.WriteLine ("Employee {0} starts working", name);
        }
    
Person

Company class:

Class company
    {public
        string name;     Company name Public
      person[] employee = new PERSON[3]; 
         
        Public Company (string cName)  //constructor
        {
            name = CName;
        }
    
        public void Run ()
        {
            Console.WriteLine ("Company" {0} "started operation", name);
            Employee[0].startwork ();
            Employee[1].startwork ();
            Employee[2].startwork ();
        }
    
        public void Stop ()
        {
            Console.WriteLine ("Company" {0} "stop working", name);
        }
    
Company

Console program:

Class program
    {
        static void Main (string[] args)
        {
            Group C = new Company ("Beijing Capacity Limited");
            C.employee[0] = new Person ("John");
            C.EMPLOYEE[1] = new Person ("Dick");
            C.EMPLOYEE[2] = new Person ("Wang er");
            C.run ();
            C.stop ();
            Console.read ();
        }
    
Program

Output:

The corresponding class diagram:

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.