The concepts and symbols of dependency relations
dependencies represent a class that relies on other classes in some way. When two classes are in a dependency relationship, the change of one of the classes may affect another class.
Symbol:
The performance of dependencies in code
In program code, a dependency means that an object of one class appears in the operation of another class. There are two common scenarios in which a class takes an object of another class as an argument for an action of its own (a formal parameter) or a local variable of an operation.
Program Demo: Code/train
Class train
{public
string number;
Public train (String num)
{number
= num;
}
public void Run ()
{
Console.WriteLine ("Train start ...) Train arrives ");
}
Train
class Person {public string name;
Public person (string name) {this.name=name; public void gotobeijing (train mytrain)//from Henan to Beijing {Console.Write (name+ "Go to Beijing to ride {0} train:", MyTrain.
number);
Mytrain.run ();
public void Gotohenan ()//from Beijing to Henan {train mytrain = new Train ("1488");
Console.Write (name + "Go to Henan ride {0} train:", Mytrain.number);
Mytrain.run (); } person
Class program
{
static void Main (string[] args)
{person
person1 = new Person ("John");
Person Person2 = new Person ("Dick");
Train train1 = new Train ("1487 times"); Henan to Beijing
train train2 = new Train ("1488 times");//Henan to Beijing
Person1. Gotobeijing (train1);
Person2. Gotobeijing (train2);
Person1. Gotohenan ();
Person2. Gotohenan ();
}
Program
The corresponding class diagram:
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/