YTU OJ 3490 C# 问题 A: C# teacher类

YTU OJ 3490 C# 问题 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());
       }
    }
}

在这里插入图片描述
注意!只需要填写缺少的代码,如下:

public Teacher(string no,string name,DateTime birthday,SexFlag sex)
        {
    
    
            this.No = no;
            this.Name = name;
            this.Birthday = birthday;
            this.Sex = sex;
            bday = birthday;       
        }

        static DateTime now = new DateTime(2019, 12, 9);//当前日期
        static DateTime bday =new DateTime();
        public string No {
    
     get; set; }
        public string Name {
    
     get; set; }
        public DateTime Birthday {
    
     set {
    
     this.birthday = value; } }
        public SexFlag Sex {
    
     get;set; }
        public string ToString()
        {
    
    
            return No+","+Name+","+(now.Year-bday.Year)+" years old,"+Sex;
        }

猜你喜欢

转载自blog.csdn.net/henishu/article/details/115048986