C# teacher類
題目描述
定義一個教師類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 string No{get { return no; }set { no = value; }}public string Name{get { return name; }set { name = value; }}public DateTime Birthday{set { birthday = value; }}public SexFlag Sex{get { return sex; }set { sex = value; }}public Teacher(string no,string name,DateTime birthday,SexFlag sex){this.no = no;this.name = name;this.birthday = birthday;this.sex = sex;}public string ToString(){string str;str = no + "," + name + ",";DateTime now = new DateTime();now = DateTime.Parse("2019-12-9");TimeSpan ts = now - birthday;int age = ts.Days / 365;str = str + age.ToString() + " years old,"+sex.ToString();return str;}
?