假定学校要统计学生的信息,包括学生姓名,学号和所在系等。要求把这些学生信息设置成学生类的属性,并通过属性访问函数来设置和读取这些信息
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace leideshuxingchengyuan
{
public class Student
{
private long id;
public static int a;
public long Id//定义属性以访问私有字段id.
{
get { return id; }//定义属性以访问私有字段id,并返回一个值存放在testnumber中
set { id = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private static string departmentName;
public static string DepartmentName
{
get { return departmentName; }
set { departmentName = value; }
}
public Student(long id, string name)
{
this.id = id;
this.name = name;
}
public long testnumber
{
get
{
return (id * 3 - 1000);
}
}
}
class Program
{
static void Main(string[] args)
{
Student.DepartmentName = "计算机学院";//静态属性赋值。
Student student = new Student(12000,"张三");
Student.a = 3;
Console.WriteLine("学生一:学号={0},姓名={1},准考证={2},所在院系={3},{4}",student.Id,student.Name,student.testnumber,Student.DepartmentName,Student.a);
Student.a += 4;
Student student1 = new Student(12001, "李四");
Console.WriteLine("学生二:学号={0},姓名={1},准考证={2},所在院系={3},{4}", student1.Id, student1.Name, student1.testnumber, Student.DepartmentName,Student.a);
}
}
}