C# get set 的使用
类中
字段是私有的 外界不能访问
属性是公开的,用于外界访问和使用
public class student { private string stuName; //字段是私有的 //属性是公开的 public string StudentName { get { return stuName;} set { stuName=value;} } private int age=10; public int Age { get{return age;} set{Age = value;} } }
public class MainClass { public static void Main() { student d1 = new student(); d1.StudentName = "John"; // Derived class property //Console.WriteLine("Name in the derived class is: {0}", d1.StudentName); Console.WriteLine("Age is: {0}", d1.Age); Console.ReadLine(); } }