題目描述
定義一個教師類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());
}
}
}
輸入
輸出
樣例輸入
copy
無
樣例輸出
0203,zhangsan,32 years old,Female
代碼:(注:不要全交,交他要的,請允許我水點博客,謝謝)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
enum SexFlag
{
Male, Female
}
class Teacher
{
private string no;
private string name;
private DateTime birthday;
private SexFlag sex = SexFlag.Male;
/////////////////////////////////////////////////////////////////
public Teacher(string a,string b,DateTime c,SexFlag d)
{
this.no = a;
this.name = b;
this.birthday = c;
this.sex = d;
}
public string ToString()
{
string sum="";
sum+=no;
sum += ",";
sum += name;
sum += ",";
int s = 2020-birthday.Year-1;
sum += s.ToString();
sum += " years old,";
sum += sex;
return sum;
}
/////////////////////////////////////////////////////////////////
}
class Program
{
static void Main(string[] args)
{
Teacher teacher = new Teacher("0203", "zhangsan", DateTime.Parse("1987-12-09"), SexFlag.Female);
Console.WriteLine(teacher.ToString());
}
}
}