C # basic syntax Learning (7 ),

Source: Internet
Author: User

C # basic syntax Learning (7 ),

This and base

The this keyword of C # indicates the current instance of the current class. this keyword is usually used to pass the current instance of the class as a parameter to other methods. Because this indicates an instance of a class, the this keyword cannot be used in static members of the class.

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 =; 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 ("member [{0}] \ t name: {1} \ t Gender: {2} \ t age: {3} \ t grade: {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 ("comment Ice", "Female", 23, 2); 99 student. joinCommunity (community); 100 101 community. displayMembers (); 102 103 Console. readLine (); 104}

Running result

Member [1] Name: Nick Gender: Male age: 25 grade: 3 member [2] Name: Jason Gender: Male age: 26 grade: 4 member [3] Name: jessice Gender: Female Age: 23 Grade: 2

The base keyword indicates the base class of the current class. You can use the base keyword to call the methods, attributes, and member variables of the base class. The following code is used:

1 public class Mammal 2 {3 public Mammal () 4 {5 age = 0; 6} 7 8 public Mammal (int a) 9 {10 age =; 11} 12 13 private int _ age; 14 public int age15 {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: Mammal26 {27 public void bark () 28 {29 base. bark (); // call the base class method 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}

Running result

Mammal bark!Dog bark!

Constructors call each other

When writing class code, you may encounter a special function call relationship, that is, the mutual calls between constructors. Constructor calls are divided into calls between different constructor of the same class and constructors of the base class called by the derived class. The former uses the this keyword, and the latter uses the base keyword.

Public class Dog: Mammal {public Dog (int a, string n): base (a) // call the base class constructor {name = n;} public Dog (): this (0, "HaHa") // calls another constructor of the same class {//} public void bark () {base. bark (); // call the Console of the base class method. writeLine ("Dog bark! ") ;}Private string _ name; public string name {get {return _ name ;}set {_ name = value ;}}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.