(6.14)练习1:建立三个类:居民、成人、官员。居民包含身份证号、姓名、出生日期,而成人继承自居民,多包含学历、职业两项数据;官员则继承自成人,多包含党派、职务两项数据。

居民类:

public class Resident
    {
        //居民包含身份证号、姓名、出生日期,
        private string id;

        private string name;
        
        private DateTime birthday;

        public string ID
        {
            get { return id; }
            set { id = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public DateTime Birthday
        {
            get { return birthday; }
            set { birthday = value; }
        }

        public virtual void SetValues(params object[] obj)
        {
            this.ID = Convert.ToString(obj[0]);
            this.Name = Convert.ToString(obj[1]);
            this.Birthday = Convert.ToDateTime(obj[2]);
        }

        public  virtual string GetValues()
        {
            return string.Format("ID:{0} | Name:{1} | Birthday:{2}",ID,Name,Birthday);
        }
        
    }
成人类:
public class Adult : Resident
    {
        //成人继承自居民,多包含学历、职业两项数据
        private string education;

        private string job;

        public string Education
        {
            get { return education; }
            set { education = value; }
        }

        public string Job
        {
            get { return job; }
            set { job = value; }
        }

        //子类重写父类方法
        public override void SetValues(params object[] obj)
        {
            this.Education = Convert.ToString(obj[0]);
            this.Job = Convert.ToString(obj[1]);
        }

        public override string GetValues()
        {
            return string.Format("Education:{0} | Job:{1}",Education,Job);
        }

    }


官员类:
public class Official:Adult
    {
        //官员则继承自成人,多包含党派、职务两项数据。
        private string partisan;

        private string post;

        public string Partisan
        {
            get { return partisan; }
            set { partisan = value; }
        }

        public string Post
        {
            get { return post; }
            set { post = value; }
        }

        public override void SetValues(params object[] obj)
        {
            this.Partisan = Convert.ToString(obj[0]);
            this.Post = Convert.ToString(obj[1]);
        }

        public override string GetValues()
        {
            return string.Format("Partisan:{0} | Post:{1}",Partisan,Post);
        }
    }
测试:
namespace Practice_6_14
{
    class Program
    {
        static void Main(string[] args)
        {
            /*
             *  8、建立三个类:居民、成人、官员。居民包含身份证号、姓名、出生日期,
                   而成人继承自居民,多包含学历、职业两项数据;官员则继承自成人,多包含党派、职务两项数据。
                   要求每个类中都提供数据输入输出的功能。
             */
            
            Resident resident = new Resident();
            resident.SetValues("12345", "程序猿",DateTime.Now);
            Console.WriteLine(resident.GetValues());

            Resident adult = new Adult();
            adult.SetValues("大学本科","程序员");
            Console.WriteLine(adult.GetValues());

            Resident official = new Official();
            official.SetValues("共产党", "人大代表");
            Console.WriteLine(official.GetValues());

            Console.ReadKey();
        }
    }
}

结果:

阅读更多

没有更多推荐了,返回首页