class Student
{
string name;
public string Name
{
get { return name; }
set { name = value; }
}
char sex;
public char Sex
{
get { return sex; }
set {
if (value == '女' || value == '男')
{
sex= value;
}
else
{
sex = '男';
}
}
}
int age;
public int Age
{
get { return age; }
set {
age = value >= 0 ? value : 0;
}
}
int chinese;
public int Chinese
{
get { return chinese; }
set {
age = value >= 0 ? value : 0;
}
}
int math;
public int Math
{
get { return math; }
set
{
age = value >= 0 ? value : 0;
}
}
int english;
public int English
{
get { return english; }
set {
age = value >= 0 ? value : 0;
}
}
public void SayHello()
{
Console.WriteLine("我叫{0},我今年{1}岁了,我是{2}同学",name ,age ,sex );
}
public void ShowScore()
{
int sum = chinese + english + math;
int avg = sum / 3;
Console.WriteLine("我叫{0},我的总成绩为{1},平均成绩为{2}",name,sum,avg);
class Program
{
static void Main(string[] args)
{
Student zsStudengt = new Student();
zsStudengt.Name = "张三";
zsStudengt .Sex ='男';
zsStudengt.Age = 18;
zsStudengt.Chinese = 90;
zsStudengt.Math = 95;
zsStudengt.English = 80;
Student xlStudent = new Student();
xlStudent.Name = "小兰";
xlStudent.Sex = '女';
xlStudent.Age = 16;
xlStudent.Chinese = 95;
xlStudent.Math = 85;
xlStudent.English = 100;
Console.WriteLine("下面是张三打招呼和显示成绩的方法");
zsStudengt.SayHello();
zsStudengt.ShowScore();
Console.WriteLine("下面是小兰打招呼和显示成绩的方法");
xlStudent.SayHello();
xlStudent.ShowScore();
Console.ReadKey();
为什么会显示:警告 1 从未对字段“属性习题.Student.chinese”赋值,字段将一直保持其默认值 0 d:\My Documents\Visual Studio 2008\Projects\入学考试练习\属性习题\Class1.cs 46 13 属性习题
|