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: