封装、继承、多态
封装体现在属性方法对外界访问的限制上;
##属性:以大写开头 与java不同
c#中属性是对private字段的封装;
class User { private int oid; public int Oid { get { if (oid == 19) { return 10; } else { return 100; } } set { if (value > 18 && value < 40) { oid = 19; } else { oid = value; } } } private string account; public string Account { get { return account; } set { account = value; } } } public static void Main(string[] args) { User u = new User(); u.Oid = 30; Console.WriteLine(u.Oid); Console.ReadKey(); //10 }
简写:
public string Account { get; set; }