核心代码出自《C#程序设计经典教程(第三版)》P142
namespace Test5_1
{
//学生类
public abstract class Student
{
protected string name;
protected int age;
public static int number;
public Student(string name, int age)
{
this.name = name;
this.age = age;
number++;
}
public string Name
{
get { return name; }
}
//public int age 1.为什么age不行
//{
// get { return age; }
//}
public virtual string type
{
get { return "学生"; }
}
public abstract double total();
public string getInfo()
{
string result = string.Format("总人数:{0},姓名:{1},{2},{3}岁", number, Name, type, age);
if (type == "小学生")
result += string.Format(",平均成绩为{0:N2};\r\n", total() / 2); //{0:N2}是什么意思?
else if (type == "中学生")
result += string.Format(",平均成绩为{0:N2};\r\n", total() / 3);
else
result += string.Format(",平均成绩为{0:N2};\r\n", total()); //换行符 \r\n
return result;
}
}
}
namespace Test5_1
{
//小学生类
public class Pupil : Student
{
protected double chinese;
protected double math;
public Pupil(string name, int age, double chinese, double math) : base(name, age)
{
this.chinese = chinese;
this.math = math;
}
public override string type //重载虚拟属性 作用是什么?
{
get
{
return "小学生";
}
}
public override double total()
{
return chinese + math;
}
}
}
namespace Test5_1
{
//中学生类
public class MiddleSchoolStudent : Student
{
protected double chinese;
protected double math;
protected double english;
public MiddleSchoolStudent(string name, int age, double chinese, double math, double english) : base(name, age)
{
this.chinese = chinese;
this.age = age;
this.english = english;
}
public override string type
{
get
{
return "中学生";
}
}
public override double total()
{
return chinese + math + english;
}
}
}
namespace Test5_1
{
//大学生类
public class Undergraduate : Student
{
protected double CompulsoryCredits; //必修学分
protected double Electives; //选修学分
public Undergraduate(string name, int age, double CompulsoryCredits, double Electives) : base(name, age)
{
this.CompulsoryCredits = CompulsoryCredits;
this.Electives = Electives;
}
//public override float total() //返回类型必须是double才能重写成员“Student.total()匹配” 所以即使是学分的类型不会是double 这里也仍用double型
//{
// return CompulsoryCredits + Electives;
//}
public override double total()
{
return CompulsoryCredits + Electives;
}
}
}
namespace Test5_1
{
public partial class Test5_1 : Form
{
public Test5_1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//需要添加容错处理功能,,如果用户输入的不是数字
int age = int.Parse(txtage.Text);
double chinese = double.Parse(txtchinese.Text);
double math = double.Parse(txtmath.Text);
//Pupil p = new Pupil(txtname.Text, age, chinese, math);// 2.未能找到类型或命名空间名"Pupil"????
Pupil p = new Pupil(txtname.Text, age, chinese, math);
lblShow.Text += p.getInfo();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
int age = int.Parse(txtage.Text);
double chinese = double.Parse(txtchinese.Text);
double math = double.Parse(txtmath.Text);
double english = double.Parse(txtenglish.Text);
MiddleSchoolStudent p = new MiddleSchoolStudent(txtname.Text, age, chinese, math, english);
lblShow.Text += p.getInfo();
}
private void button3_Click(object sender, EventArgs e)
{
int age = int.Parse(txtage.Text);
double CompulsoryCredits = double.Parse(txtchinese.Text);
double Electives = double.Parse(txtmath.Text);
// Undergraduate p = new Undergraduate(txtname.Text, age, CompulsoryCredits, Electives);// 3.无法从double转化成float
Undergraduate p = new Undergraduate(txtname.Text, age, CompulsoryCredits, Electives);
lblShow.Text += p.getInfo();
}
}
}
实验5_1:
原因:windows下“\n”不能实现换行,将其改为“\r\n”后可实现换行