UML學習:類圖class的依賴關係

來源:互聯網
上載者:User

依賴關係的概念與符號

依賴表示一個類以某種形式依賴於其他類。當兩個類處於依賴關係中時,其中一個類的改變可能會影響另一個類。

符號:

依賴關係在代碼中的表現

在程式碼中,依賴關係意味著一個類的對象出現在另一個類的操作中。常見的有兩種情況:一個類將另一個類的對象作為自己某個操作的參數(形參),或者是操作的局部變數。

程式示範:code/train

class train    {        public string number;            public train(string num)        {            number = num;        }            public void run()        {            Console.WriteLine("列車啟動……列車到站");        }    }    train
class person    {        public string name;        public person(string name)        {            this.name=name;        }        public void GoToBeijing(train mytrain)  //從河南到北京        {            Console.Write(name+"去北京乘坐{0}列車:",mytrain.number);            mytrain.run();        }        public void GoToHenan()   //從北京到河南        {            train mytrain = new train("1488");             Console.Write(name + “去河南乘坐{0}列車:", mytrain.number);            mytrain.run();        }    }    Person
class Program    {        static void Main(string[] args)        {            person person1 = new person("張三");            person person2 = new person("李四");                train train1 = new train("1487次"); //河南到北京        train train2 = new train(“1488次”); //河南到北京        person1.GoToBeijing(train1);            person2.GoToBeijing(train2);            person1.GoToHenan();            person2.GoToHenan();        }    }    Program

對應的類圖:

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.