C#——this關鍵字(2)(含求助貼)。本站提示廣大學習愛好者:(C#——this關鍵字(2)(含求助貼))文章只能為提供參考,不一定能成為您想要的結果。以下是C#——this關鍵字(2)(含求助貼)正文
這次來看一看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!