C#根本语法学习(七)
C#基本语法学习(七)
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; } } }