public static void Show() { PersonCtor person_without_param = new PersonCtor(); Util.PrintTitle(delegate() { person_without_param.OutputInfo(); }, "无参数实例构造函数"); PersonCtor person_with_param = new PersonCtor("siwing", 101, "Male", 19596); Util.PrintTitle(delegate() { person_with_param.OutputInfo(); }, "带参数实例构造函数"); Util.PrintTitle(delegate() { SubClass.DoConstructorInheritance(); }, "子类调用基类的构造函数"); //try //{ // 该语句会导致编译不通过 // // Console.WriteLine(person_with_param.static_var); // Console.WriteLine(PersonCtor.static_var); //} //catch //{ // Console.WriteLine("发生异常"); //} Util.PrintTitle(delegate() { PersonStaticConstructor.Show(); }, "静态构造函数"); Util.PrintTitle(delegate() { PersonAttr.Show(); }, "属性"); Util.PrintTitle(delegate() { IndexerArray.Show(); }, "索引器"); Util.PrintTitle(delegate() { TypeConvert.ShowConvert(); }, "类型转换"); }
public static void Show() { PersonAttr person_attr = new PersonAttr(); Console.WriteLine(person_attr.Name); person_attr.Name = "set_name"; Console.WriteLine($"person_attr name is '{person_attr.Name}'"); /* person_attr.Gender 只能赋值为"male"或"female" */ try { person_attr.Gender = "error"; Console.WriteLine(person_attr.Gender); } catch (InvailValueException e) { Console.WriteLine($"发生异常: {e}"); } }