這次來看一看this關鍵字的第二個用法:將對象作為參數傳遞到其他方法
----------------------------------------------------------------------------------
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 //我們假設要把一個學生的成績按70%折算
8
9 namespace @this
10 {
11 class Program
12 {
13 static void Main(string[] args)
14 {
15 Student student = new Student();
16 student.GetMessage("Mark");
17 student.PrintScore ();
18 }
19 }
20
21 class Student
22 {
23 public string Name { get; set; }
24 private int Score = 100;
25
26 public void GetMessage(string Name)
27 {
28 //這個this用法屬於第一種:限定被相似的名稱隱藏的成員
29 this.Name = Name;
30 }
31
32 public int score
33 {
34 get { return Score; }
35 }
36
37 public void PrintScore()
38 {
39 Console.WriteLine("My Name Is {0}, My Score Is {1}",Name,score );
40 Console.WriteLine("How many points after converting? ");
41 Console.WriteLine("The Converted Score Is {0}",Convert .ConvertedScore(this));//注意:這裡要用this傳參數了
42 }
43 }
44
45 class Convert
46 {
47 public static int ConvertedScore(Student stu)//折算分數
48 {
49 return (int)(0.7 * stu.score);//強制類型轉換一下
50 }
51 }
52
53 }
41行代碼 Convert .ConvertedScore(this) 裡面的this也便就是“折算後的分數”
說實話,我對this關鍵字的這個用法理解的並不是太透徹,用的時候也是雲裡霧裡的,所以希望網友們能夠積極的給我評論,給予我一些幫助,給我講解一下這個地方,在下感激不盡;)----------To be continued!