C # teacher class

Title Description

Define a class teacher Teacher, specific requirements are as follows:

1, private field work number no (string), name name (string), date of birth birthday (DateTime), gender sex (SexFlag). Which, SexFlag as enumerated types, including Male (for male), Female (for female), and the field defaults to male sex.

2, the definition of public use to access the read-write property No no field; read and write the definition of public property Name used to access the name field; the definition of public property only Birthday to write assignments birthday field; read and write the definition of public property Sex used to access the sex field.

3, reasonable design constructor, makes it possible to set the job number, name, date of birth, gender when creating objects.

4, rewriting ToString () method for Teacher output object, the following format is described.

5, create a teacher Object teacher (--0203 job number, name --zhangsan, --1987-12-09 date of birth, gender - female), call the ToString () method to display the information on the teacher console:


According to the code, the code written up missing.

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());
        }
    }
}

 
 


Entry

no

Export

Sample input

 

Sample Output

0203,zhangsan,32 years old,Female

 

using System;
namespace ConsoleApplication1
{
    enum SexFlag
    {
        Male, Female
    }
    class Teacher
    {
        private string no;
        private string name;
        private DateTime birthday;
        private SexFlag sex = SexFlag.Male;
        public string No
        {
            get
            {
                return no;
            }
            set
            {
                no = value;
            }
        }
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        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;
            }
        }
        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());
        }
    }
}

 

Published 45 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/DreamTrue1101/article/details/105015714