面向對象

這篇篇文章我主要經過兩個例子(計算器和猜拳遊戲)來理解面向對象。算法

使用面向對象編程,關鍵的是要找出重複的東西,而後把它抽象出來,進行封裝。因此,咱們先來看下計算器計算的時候,哪些是重複的。重複的可做爲屬性。編程

先看計算器的例子。咱們先模擬下計算的場景。當用戶打開計算器的時候,先須要輸入第一個要計算的數字,而後選擇運算符,接下來輸入第二個要計算的數字,最後得出計算結果。這裏面,每次計算的時候,都涉及到要輸入的數字,運算符,這三個是變量,因此咱們把它們抽象出來,放到一個計算類Calc中。而後在點擊計算的時候運算便可。代碼以下:dom

Calc類:ide

 1 public class Calc
 2     {
 3         public double Number1 { get; set; }
 4         public double Number2 { get; set; }
 5 
 6         public double Calculate(string sign)
 7         {
 8             double r;
 9             switch (sign)
10             {
11                 case "+":
12                     r = this.Number1 + this.Number2;
13                     break;
14                 case "-":
15                     r = this.Number1 - this.Number2;
16                     break;
17                 case "*":
18                     r = this.Number1 * this.Number2;
19                     break;
20                 case "/":
21                     r = this.Number1 / this.Number2;
22                     break;
23                 default:
24                     throw new Exception("參數錯誤!");
25             }
26             return r;
27         }
28     }
View Code

計算方法:優化

 1 namespace 面線對象實現計算器
 2 {
 3     public partial class Form1 : Form
 4     {
 5         public Form1()
 6         {
 7             InitializeComponent();
 8         }
 9 
10         private void btnEquals_Click(object sender, EventArgs e)
11         {
12             Calc c = new Calc();
13             c.Number1 = Convert.ToDouble(txtNumber1.Text);
14             c.Number2 = Convert.ToDouble(txtNumber2.Text);
15 
16             string sign = cmbSign.Text;
17 
18             try
19             {
20                 txtResult.Text = c.Calculate(sign).ToString();
21             }
22             catch (Exception ex)
23             {
24                 MessageBox.Show(ex.Message);
25             }
26 
27         }
28     }
29 }
View Code

 接下來咱們看下面向過程的編程。this

 1 public partial class Form2 : Form
 2     {
 3         public Form2()
 4         {
 5             InitializeComponent();
 6         }
 7 
 8         private void btnEquals_Click(object sender, EventArgs e)
 9         {
10             double n1 = Convert.ToDouble(txtNumber1.Text); 
11             double n2 = Convert.ToDouble(txtNumber2.Text);
12             try
13             {
14                 string sign = cmbSign.Text;
15                 double r;
16                 switch (sign)
17                 {
18                     case "+":
19                         r = n1 + n2;
20                         break;
21                     case "-":
22                         r = n1 - n2;
23                         break;
24                     case "*":
25                         r = n1 * n2;
26                         break;
27                     case "/":
28                         r = n1 / n2;
29                         break;
30                     default:
31                         throw new Exception("參數錯誤!");
32                 }
33 
34                 txtResult.Text = r.ToString();
35             }
36             catch (Exception ex)
37             {
38                 MessageBox.Show(ex.Message);
39             }
40         }
41     }
View Code

這樣看來面向對象和麪向過程好像沒什麼區別,那爲何還要用面向對象呢?假如,我如今不想用WinForm窗口實現計算器,想改爲控制檯程序。若是用面向對象編程,只要把Calc類複製過去就實現了整個算法,而面向過程卻要把calc中算法所有重寫。兩者的區別在這裏就開始體現了。固然這個計算器還能進一步優化,這個後續再說。spa

接下來看猜拳遊戲。3d

首先模擬下游戲的場景。當用戶點擊剪刀時,你出:顯示剪刀,電腦隨機出,而後比較輸贏。這裏涉及到的對象有你,電腦。涉及到的屬性有,剪刀,石頭,布,每一個對象都出拳的方法。每一個角色都要有個保存出拳結果的屬性。既然電腦和你的屬性和方法都相同,那可否把這兩個對象合併成一個對象呢?最好不要。由於你出拳是用戶在界面點擊,而後把參數傳進去,電腦是隨機出拳,能夠在方法中隨機生成,不傳參數,兩個方法的參數不一致,分開更好維護。比較輸贏的方法不能存在於你和電腦中,由於裁判必須是第三方。爲方便判斷輸贏,咱們把出拳結果用數字表示,石頭=1,剪刀=2,布=3,用用戶出拳減去電腦出拳的結果來表示輸贏,很容易得出用戶-電腦=-1或者2的時候,玩家贏,用戶-電腦=0的時候,表示平局,其他是玩家輸。因此,整個猜拳遊戲的代碼以下:code

出拳的枚舉:orm

1 namespace 猜拳遊戲
2 {
3     public enum Fist
4     {
5         stone=1,
6         scissors=2,
7         cloth=3
8     }
9 }
View Code

玩家類:

 1 namespace 猜拳遊戲
 2 {
 3     public class Person
 4     {
 5         public int PFist { get; set; }
 6 
 7         public void ShowFist(int index)
 8         {
 9             this.PFist = index;
10         }
11     }
12 }
View Code

電腦類:

 1 namespace 猜拳遊戲
 2 {
 3     public class Computer
 4     {
 5         public int CFist { get; set; }
 6 
 7         public void ShowFist()
 8         {
 9             Random rd = new Random();
10             int r =rd.Next(1,4);
11             this.CFist = r;
12         }
13     }
14 }
View Code

裁判類:

 1 namespace 猜拳遊戲
 2 {
 3     public class Judgement
 4     {
 5         /// <summary>
 6         /// 猜拳判斷輸贏 玩家贏=1;平局=0;玩家輸=-1
 7         /// </summary>
 8         /// <param name="p">玩家</param>
 9         /// <param name="c">電腦</param>
10         /// <returns></returns>
11         public int Judge(int p,int c)
12         {
13             int r;
14             if (p - c == -1 || p - c == 2)
15             {
16                 r = 1;
17             }
18             else if (p - c == 0)
19             {
20                 r = 0;
21             }
22             else
23             {
24                 r = -1;
25             }
26             return r;
27         }
28     }
29 }
View Code

這裏要特別提示下:

裁判判斷的結果是輸贏,這裏最好不要直接返回相似「你贏了」之類的信息,返回數字便可,由於用戶可能會把「你贏了」改爲「You Win !」等任何其餘提示信息。

 窗體類中的代碼:

 1 private void btnGuessStone_Click(object sender, EventArgs e)
 2         {
 3             Computer c = new Computer();
 4             c.ShowFist();
 5             lblComputerResult.Text = c.CFist == (int)Fist.stone ? "石頭" : (c.CFist == (int)Fist.scissors ? "剪刀" : "");
 6             
 7 
 8             Person p = new Person();
 9             p.ShowFist((int)Fist.stone);
10             lblYouResult.Text = p.PFist == (int)Fist.stone ? "石頭" : "";
11 
12             Judgement j = new Judgement();
13             int r= j.Judge(p.PFist,c.CFist);
14 
15             string result = string.Empty;
16             //猜拳判斷輸贏,返回值=0:平局;返回值的絕對值=1:玩家贏;返回值的絕對值=2:玩家輸。
17             if (r == 0)
18             {
19                 result = "平局,再來一局!";
20             }
21             if (r==1)
22             {
23                 result = "你贏了!!!";
24             }
25             if (r == -1)
26             {
27                 result = "你輸了!!!";
28             }
29 
30             lblResult.Text = result;
31         }
32 
33         private void btnGuessScissors_Click(object sender, EventArgs e)
34         {
35             Computer c = new Computer();
36             c.ShowFist();
37             lblComputerResult.Text = c.CFist == (int)Fist.stone ? "石頭" : (c.CFist == (int)Fist.scissors ? "剪刀" : "");
38             
39 
40             Person p = new Person();
41             p.ShowFist((int)Fist.scissors);
42             lblYouResult.Text = p.PFist == (int)Fist.scissors ? "剪刀" : "";
43 
44             Judgement j = new Judgement();
45             int r = j.Judge(p.PFist, c.CFist);
46 
47             string result = string.Empty;
48             //猜拳判斷輸贏,返回值=0:平局;=1:玩家贏;-1:玩家輸。
49             if (r == 0)
50             {
51                 result = "平局,再來一局!";
52             }
53             if (r == 1)
54             {
55                 result = "你贏了!!!";
56             }
57             if (r == -1)
58             {
59                 result = "你輸了!!!";
60             }
61 
62             lblResult.Text = result;
63         }
64 
65         private void btnGuessCloth_Click(object sender, EventArgs e)
66         {
67             Computer c = new Computer();
68             c.ShowFist();
69             lblComputerResult.Text = c.CFist == (int)Fist.stone ? "石頭" : (c.CFist == (int)Fist.scissors ? "剪刀" : "");
70             
71 
72             Person p = new Person();
73             p.ShowFist((int)Fist.cloth);
74             lblYouResult.Text = p.PFist == (int)Fist.cloth ? "" : "";
75 
76             Judgement j = new Judgement();
77             int r = j.Judge(p.PFist, c.CFist);
78 
79             string result = string.Empty;
80             //猜拳判斷輸贏,返回值=0:平局;1:玩家輸;2:玩家贏。
81             if (r == 0)
82             {
83                 result = "平局,再來一局!";
84             }
85             if (r == 1)
86             {
87                 result = "你贏了!!!";
88             }
89             if (r == -1)
90             {
91                 result = "你輸了!!!";
92             }
93 
94             lblResult.Text = result;
95         }
View Code

以上就是面向對象的兩個例子,可能表述的不是很完整,後期會反覆雕琢,歡迎指正!

相關文章
相關標籤/搜索