問題 A: 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());
}
}
}
輸入
輸出
注意是提交部分代碼(GAN!),格式自己調
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
enum SexFlag
{
Male, Female
}
class Teacher
{
private string no;
public string No
{
get
{
return no;
}
set
{
no = value;
}
}
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
private DateTime birthday;
public int olds;
String ts = DateTime.Now.Year.ToString();
public DateTime Birthday
{
get
{
return birthday;
}
set
{
birthday = value;
olds = int.Parse(ts) - birthday.Year - 1;
}
}
private SexFlag sex = SexFlag.Male;
public SexFlag Sex
{
get
{
return sex;
}
set
{
sex = value;
}
}
private string v1;
private string v2;
private DateTime dateTime;
private SexFlag female;
public override string ToString()
{
return no+","+ name+","+ olds +" years old" + ","+sex;
}
public Teacher(string v1, string v2, DateTime dateTime, SexFlag female)
{
this.No = v1;
this.Name = v2;
this.Birthday = dateTime;
this.Sex = female;
}
}
class Program
{
static void Main(string[] args)
{
Teacher teacher = new Teacher("0203", "zhangsan", DateTime.Parse("1987-12-09"), SexFlag.Female);
Console.WriteLine(teacher.ToString());
}
}
}