C#類型成員:方法


一、方法

  方法的基本結構:返回值 方法名(參數){ 內容 },其中無返回值時用void,有返回值時用返回值類型,參數可以是零到無限個,參數由參數類型和參數名組成。

1 void Method1() { }                      //無返回值、無參數的方法
2 void Method2(int i) { }                 //無返回值、有一個參數的方法
3 int Method3(bool b, string s) { }       //返回值為int、有二個參數的方法
4 class class1 { }                        //定義一個類作為參數或者返回值
5 class1 Method4(float f, class1 c) { }   //返回值為class、有二個參數的方法

  方法又稱為函數,它既可以作為一個獨立的功能,又可以作為類的行為。

  作為獨立功能:洗衣機

 1 /// <summary>
 2 /// 洗衣機的功能
 3 /// </summary>
 4 /// <param name="clothing">投放衣物</param>
 5 /// <param name="detergent">投放洗滌劑(單位:克)</param>
 6 /// <param name="wash">選擇洗漂(true或false)</param>
 7 /// <param name="dehydration">選擇脫水(true或false)</param>
 8 /// <param name="duration">設定時長(單位:分鍾)</param>
 9 /// <returns>返回結果</returns>
10 public string WashingMachine(string clothing, int detergent, bool wash, bool dehydration, int duration)
11 {
12     if (clothing == null)
13     {
14         return "請放入衣物";
15     }
16     if (duration < 5)
17     {
18         return "洗衣時長不能小於5分鍾";
19     }
20     else
21     {
22         float washTime;         //洗漂時長
23         float dehydrationTime;  //脫水時長
24         if (wash)
25         {
26             washTime = dehydration ? duration * 2 / 3 : duration;
27             if (detergent <= 0)
28             {
29                 return "洗漂需要投放洗滌劑";
30             }
31             else
32             {
33                 //開始洗漂衣物
34             }
35         }
36         if (dehydration)
37         {
38             dehydrationTime = wash ? duration - washTime : duration;
39             //開始脫水衣物
40         }
41         return "完成";
42     }
43 }

  作為類的行為:洗衣機

 1 /// <summary>
 2 /// 洗衣機的類
 3 /// </summary>
 4 public class WashingMachine
 5 {
 6     #region 字段
 7     /// <summary>
 8     /// 時長(單位:分鍾)
 9     /// </summary>
10     public int duration;
11     #endregion
12 
13     #region 方法
14     /// <summary>
15     /// 投放
16     /// </summary>
17     /// <param name="clothing">衣物</param>
18     /// <param name="detergent">洗滌劑(單位:克)</param>
19     public void ThrowIn(string clothing, int detergent) { }
20 
21     /// <summary>
22     /// 洗脫功能
23     /// </summary>
24     public void All() { }
25 
26     /// <summary>
27     /// 洗漂功能
28     /// </summary>
29     public void Wash() { }
30 
31     /// <summary>
32     /// 脫水功能
33     /// </summary>
34     public void dehydration() { }
35 
36     /// <summary>
37     /// 設定時長
38     /// </summary>
39     /// <param name="duration">時長(單位:分鍾)</param>
40     public void SettingTime(int duration) { }
41 
42     /// <summary>
43     /// 開啟洗衣機
44     /// </summary>
45     public void Open() { }
46 
47     /// <summary>
48     /// 關閉洗衣機
49     /// </summary>
50     public void Close() { }
51     #endregion
52 }

  方法作為獨立功能對比作為類的行為優點是代碼少、運行快,缺點則是維護性和擴展性差;

  方法作為類的行為對比作為獨立功能缺點是代碼多、運行慢,優點則是維護性和擴展性好。

  C#中有一種功能特殊的方法:構造函數。

 二、方法的重載

  當多個方法所做的事相同,但是參數的個數和參數的類型不同時,會出現同一件事方法名不同,這樣既不利於記憶也不利於維護,所以C#支持方法名相同,但是參數個數或者參數類型不同的方法,這種方法叫方法的重載(參數個數相同,類型不同時,參數順序不同也是重載)。

  比如洗衣機的投放方法,當我只想脫水的時候,我只想傳入一個參數衣物;有時我需要衣物按件來放,而不是傳入衣物。

 1 public class WashingMachine
 2 {
 3     /// <summary>
 4     /// 投放
 5     /// </summary>
 6     /// <param name="clothes">衣物件數</param>
 7     public void ThrowIn(int clothes) { }
 8 
 9     /// <summary>
10     /// 投放
11     /// </summary>
12     /// <param name="clothing">衣物</param>
13     public void ThrowIn(string clothing) { }
14 
15     /// <summary>
16     /// 投放
17     /// </summary>
18     /// <param name="clothes">衣物件數</param>
19     /// <param name="detergent">洗滌劑(單位:克)</param>
20     public void ThrowIn(int clothes, int detergent) { }
21 
22     /// <summary>
23     /// 投放
24     /// </summary>
25     /// <param name="clothing">衣物</param>
26     /// <param name="detergent">洗滌劑(單位:克)</param>
27     public void ThrowIn(string clothing, int detergent) { }
28 
29     /// <summary>
30     /// 投放(參數個數相同,類型不同時,參數順序不同)
31     /// </summary>
32     /// <param name="clothing">衣物</param>
33     /// <param name="detergent">洗滌劑(單位:克)</param>
34     public void ThrowIn(int detergent, string clothing) { }
35 }

  關聯:方法的重載與方法的重寫的對比(見C#多態)

  后期補充。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM