面向对象方法的基本编程 (code)
/*--输入N个学生的4门课程成绩,比较总成绩大小. (目前只实例了两个学生)--*/
namespace _0_11_02
{
class Student
{
//定义一个保存用户名的私有变量, 为成员。
string _name;
/// <summary>
/// 封装一个属性 代表学生姓名
/// </summary>
public string Name
{
get { return _name; }
set { _name = value; }
}
/// <summary>
/// 封装一个方法 计算总成绩
/// </summary>
/// <returns>返回总成绩</returns>
public int result()
{
int i, sum=0;
for (i = 0; i < 4; i++ )
{
sum += this._garde[i];
}
return sum;
}
//定义一个一维数组保存成绩。
public int[] _garde = new int[4];
/// <summary>
/// 构造函数 取得4门课程的每个成绩
/// </summary>
public Student()
{
Console.WriteLine("输入一个学生的4门课程的成绩:");
int i;
for (i = 0; i < 4; i++ )
{
this._garde[i] = int.Parse(Console.ReadLine());
}
}
}
class Test
{
static void Main()
{
Student stu1 = new Student();
Student stu2 = new Student();
//判断学生成绩大小
if(stu1.result() > stu2.result())
{
Console.WriteLine("成绩最好的是{0}",stu1.result());
}
else
{
Console.WriteLine("成绩最好的是{0}",stu2.result());
}
}
}
}

浙公网安备 33010602011771号