using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 封装 { class Program { static void Main(string[] args) { student s = new student();//实列化 s.write(); #region s.code = "101"; s.score = 101; s.birthay = DateTime.Now; Console.WriteLine(s.code); Console.WriteLine(s.score); Console.WriteLine(s.birthay); Console.WriteLine(s.birthdaystr); Console.WriteLine(s.codeandscormsum); #endregion Console.ReadLine(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 封装 { class student { private string _code;//prevate 访问修饰符 并不是所有的 //具体化 public string code//public 访问权限最高的 所有的都可访问 { get{ return _code;} set { _code = value;} } private string _name; public string name { get; set; }//简写 private decimal _score; public decimal score { get { return _score; } set { if (value < 0 || value > 100)//确定取值范围 { _score = 0; } else { _score = value; } } } private DateTime _birthday; public DateTime birthay { get { return _birthday; } set { _birthday = value; } } public string birthdaystr { get { return _birthday.ToString("yyyy年mm月dd日"); } } public string codeandscormsum { get { return _code + _score; } } //成员方法 public void write() { Console.WriteLine("我是stu的write方法!"); } } }