this和base
C#的this關鍵字表示當前類的當前實例,this關鍵字通常用於把類的當前實例作為參數傳遞給別的方法。由於this表示一個類的實例,所以在類的靜態成員中,不能使用this關鍵字。
1 public class Student 2 { 3 public Student(string n, string s, int a, int g) 4 { 5 name = n; 6 sex = s; 7 age = a; 8 grade = g; 9 } 10 11 private string _name; 12 public string name 13 { 14 get { return _name; } 15 set { _name = value; } 16 } 17 18 private string _sex; 19 public string sex 20 { 21 get { return _sex; } 22 set { _sex = value; } 23 } 24 25 private int _age; 26 public int age 27 { 28 get { return _age; } 29 set { _age = value; } 30 } 31 32 private int _grade; 33 public int grade 34 { 35 get { return _grade; } 36 set { _grade = value; } 37 } 38 39 public bool joinCommunity(StudentCommunity sc) 40 { 41 return sc.addMember(this); 42 } 43 44 } 45 46 public class StudentCommunity 47 { 48 private const int MaxStudents = 100; 49 private Student[] members = new Student[MaxStudents]; 50 51 private string _name; 52 public string name 53 { 54 get { return _name; } 55 set { _name = value; } 56 } 57 58 private int _count = 0; 59 public int count 60 { 61 get { return _count; } 62 } 63 64 public bool addMember(Student s) 65 { 66 if (count < MaxStudents) 67 { 68 members[count] = s; 69 _count++; 70 71 return true; 72 } 73 else 74 { 75 return false; 76 } 77 } 78 79 public void displayMembers() 80 { 81 for (int i = 0; i < count; i++) 82 { 83 Student s = members[i]; 84 Console.WriteLine("成員[{0}]\t姓名:{1}\t性別:{2}\t年齡:{3}\t年級:{4}", i + 1, s.name, s.sex, s.age, s.grade); 85 } 86 } 87 } 88 89 static void Main(string[] args) 90 { 91 StudentCommunity community = new StudentCommunity(); 92 93 community.name = "Basketball Community"; 94 Student student = new Student("Nick", "Male", 25, 3); 95 student.joinCommunity(community); 96 student = new Student("Jason", "Male", 26, 4); 97 student.joinCommunity(community); 98 student = new Student("Jessice", "Female", 23, 2); 99 student.joinCommunity(community); 100 101 community.displayMembers(); 102 103 Console.ReadLine(); 104 }
運行結果
成員[1] 姓名:Nick 性別:Male 年齡:25 年級:3 成員[2] 姓名:Jason 性別:Male 年齡:26 年級:4 成員[3] 姓名:Jessice 性別:Female 年齡:23 年級:2
base關鍵字表示當前類的基類。可以用base關鍵字調用基類的方法、屬性和成員變量。如下代碼所示:
1 public class Mammal 2 { 3 public Mammal() 4 { 5 age = 0; 6 } 7 8 public Mammal(int a) 9 { 10 age = a; 11 } 12 13 private int _age; 14 public int age 15 { 16 get { return _age; } 17 set { _age = value; } 18 } 19 20 public void bark() 21 { 22 Console.WriteLine("Mammal bark!"); 23 } 24 } 25 public class Dog:Mammal 26 { 27 public void bark() 28 { 29 base.bark();//調用基類方法 30 Console.WriteLine("Dog bark!"); 31 } 32 } 33 static void Main(string[] args) 34 { 35 Dog dog = new Dog(); 36 37 dog.bark(); 38 39 Console.ReadLine(); 40 }
運行結果
Mammal bark! Dog bark!
構造函數相互調用
在編寫類代碼時會遇到一種特殊的函數調用關系,就是構造函數之間的相互調用。構造函數相互調用分為同一個類的各個不同構造函數之間的調用和派生類調用基類的構造函數,前者使用this關鍵字,後者使用base關鍵字。
public class Dog:Mammal { public Dog(int a, string n) :base(a) //調用基類構造函數 { name = n; } public Dog() :this(0, "HaHa") //調用相同類的另外一個構造函數 { //// } public void bark() { base.bark();//調用基類方法 Console.WriteLine("Dog bark!"); } private string _name; public string name { get { return _name; } set { _name = value; } } }