using System.Runtime.Intrinsics.Arm; namespace Lesson5_封装_成员属性练习 { class Student { private int age,csharpscore,unityscore; private string sex; public Student() { } public Student(string name,int age,string sex,int csharp,int unity) { this.Name = name; this.Sex = sex; this.Age = age; this.CsharpScore = csharp; this.UnityScore = unity; } public string Name { get; set; } public string Sex { get { return sex; } set { if (value == "男" || value == "女") { sex = value; } else { sex = "不男不女"; } } } public int Age { get { return age; } set { if (value < 0) { age = 0; Console.WriteLine("年龄不在范围内"); }else if (value > 150) { age = 150; Console.WriteLine("年龄不在范围内"); }else { age = value; } } } public int CsharpScore { get { return csharpscore; } set { if (value < 0 || value > 100) { csharpscore = 0; Console.WriteLine("分数不在范围内"); } else { csharpscore = value; } } } public int UnityScore { get { return unityscore; } set { if (value < 0 || value > 100) { unityscore = 0; Console.WriteLine("分数不在范围内"); } else { unityscore = value; } } } public void Greet() { Console.WriteLine("我叫{0}, 今年{1}岁,是{2}生", Name, Age, Sex); } public void ShowTotalScore() { int total = CsharpScore + UnityScore; float avg = total / 2f; Console.WriteLine("你的总分是:"+total+"平均分是:"+avg); } } class Program { static void Main(string[] args) { Console.WriteLine("Test1:"); Student s1 = new Student(); s1.Name = "Test1"; s1.Sex = "男"; s1.Age = 123; s1.CsharpScore = 60; s1.UnityScore = 60; s1.Greet(); s1.ShowTotalScore(); Console.WriteLine("Test2:"); Student s2 = new Student("test2",666,"超人",60,666); s2.Greet(); s2.ShowTotalScore(); } } }