題目描述
定義一個教師類Teacher,具體要求如下:
1、私有字段工號no(string)、姓名name(string)、出生日期birthday(DateTime)、性別sex(SexFlag)。其中,SexFlag爲枚舉類型,包括Male(表示男性)、Female(表示女性),並且字段sex缺省值爲男。
2、定義公有讀寫屬性No用來訪問no字段;定義公有讀寫屬性Name用來訪問name字段;定義公有隻寫屬性Birthday用來賦值birthday字段;定義公有讀寫屬性Sex用來訪問sex字段。
3、設計合理的構造函數,使得創建對象時可以設置工號、姓名、出生日期、性別。
4、重寫ToString()方法,用來輸出Teacher對象的信息,具體格式如下描述。
5、創建一個教師對象teacher(工號--0203, 姓名--zhangsan,出生日期--1987-12-09 , 性別--女),調用ToString()方法後在控制檯上顯示teacher信息:
根據以下代碼,請補寫缺失的代碼。
using System;
namespace ConsoleApplication1
{
enum SexFlag
{
Male,Female
}
class Teacher
{
private string no;
private string name;
private DateTime birthday;
private SexFlag sex = SexFlag.Male;
/////////////////////////////////////////////////////////////////
//請填寫代碼
/////////////////////////////////////////////////////////////////
}
class Program
{
static void Main(string[] args)
{
Teacher teacher = new Teacher("0203", "zhangsan", DateTime.Parse("1987-12-09"), SexFlag.Female);
Console.WriteLine(teacher.ToString());
}
}
}
輸入
輸出
樣例輸入
無
樣例輸出
0203,zhangsan,32 years old,Female
public Student(string s, string t)
{
studentid = s;
studentname = t;
}
public DateTime BirthDate
{
get { return birthdate; }
set { birthdate = value; }
}
public string BirthPlace
{
get { return birthplace; }
set { birthplace = value; }
}
public string StudentName
{
get { return studentname; }
set { studentname = value; }
}
public string StudentId
{
get { return studentid; }
set { studentid = value; }
}
public int Age
{
get { return DateTime.Now.Year - birthdate.Year + 1; }
}