用C# 定义一个表示人员的Person 抽象类,其中包括姓名,编号,性别3个数据成员,以及输出人员函

2025-03-12 22:33:50
推荐回答(2个)
回答1:

abstract class Person
{
protected string _name;
protected string _sex;
protected string _no;
public abstract void output(string s);
}
class Student:Person
{
private double[] scores = new double[3];

public double[] Scores
{
get { return scores; }
set { scores = value; }
}
public Student(string name)
{
this._name = name;
}
public void SetScores()
{
for(int i=0;i<3;i++)
{
scores[i] = double.Parse(Console.ReadLine());
}
}

public override void output(string s)
{
Console.WriteLine(s);
}
public void CompareTo(Student b)
{
double totalA=0.0, totalB=0.0;
for (int i = 0; i < 3; i++)
{
totalA+= this.scores[i];
totalB += b.Scores[i];
}
int s= totalA.CompareTo(totalB);
string result = "总分多的是:";
if (s > 0)
{ result = result + this._name; }
else if (s < 0)
{ result = result + b._name; }
else
{ result = "分数一样"; }
output(result);
}
}

class Teacher:Person
{
private int teachAge;

public Teacher(string name)
{
_name = name;
}
public int TeachAge
{
get { return teachAge; }
set { teachAge = value; }
}
public override void output(string s)
{
Console.WriteLine(s);
}
public void CompareTo(Teacher b)
{
int age = this.TeachAge.CompareTo(b.TeachAge);
string result="教龄大的是:";
if (age > 0)
{ result =result+ this._name; }
else if (age<0)
{ result =result+b._name; }
else
{ result = "教龄一样"; }
output(result);
}
}

static void Main()
{
Student sa = new Student("StudentA");
Student sb = new Student("StudentB");
sa.Scores = new double[] { 70, 80, 90 };
sb.Scores = new double[] { 60, 80, 100 };
sa.CompareTo(sb);

Teacher ta = new Teacher("TeacherA");
Teacher tb = new Teacher("TeacherB");
ta.TeachAge = 19;
tb.TeachAge = 19;
ta.CompareTo(tb);

Console.Read();
}

回答2:

不好意思还没学到这儿