每日博客

C#

设计编写一个控制台应用程序,练习类的继承。

(1) 编写一个抽象类 People,具有”姓名”,”年龄”字段,”姓名”属性,Work 方法。

(2) 由抽象类 People 派生出学生类 Student 和职工类 Employer,继承 People 类,并

覆盖Work 方法。

(3) 派生类 Student 增加”学校”字段,派生类 Employer 增加”工作单位”字段。

(4) 在 Student 和 Employer 实例中输出各自不同的信息。

using System;

namespace test

{

abstract class People  

    {

        public String name;

        public String Name {get;set;}

        public int age;

        public abstract void work();

    }

    class Student : People 

    {

        public String school;

        public override void work()

        {

            Console.WriteLine("student类");

        }

    }

    class Employer : People

    {

        public String workplace;

        public override void work()

        {

            Console.WriteLine("Employer类");

        }

    }

    class main

    {

        static void Main(string[] args)

        {

            Student st = new Student();

            Console.WriteLine("请输入学生的姓名:");

            st.name = Console.ReadLine();

            Console.WriteLine("请输入学生的昵称:");

            st.Name = Console.ReadLine();

            Console.WriteLine("请输入学生的年龄:");

            st.age = int.Parse(Console.ReadLine());

            Console.WriteLine("请输入学生的学校:");

            st.school = Console.ReadLine();

            Console.WriteLine("学生的姓名:{0}", st.name);

            Console.WriteLine("学生的小名:{0}", st.Name);

            Console.WriteLine("学生的年龄:{0}", st.age);

            Console.WriteLine("学生的学校:{0}", st.school);

            st.work();

            Employer em = new Employer();

            Console.WriteLine("请输入职工的姓名:");

            em.name = Console.ReadLine();

            Console.WriteLine("请输入职工的年龄:");

            em.age = int.Parse(Console.ReadLine());

            Console.WriteLine("请输入职工的工作地点:");

            em.workplace = Console.ReadLine();

            Console.WriteLine("职工的姓名:{0}", em.name);

            Console.WriteLine("职工的年龄:{0}", em.age);

            Console.WriteLine("职工的工作地点:{0}", em.workplace);

            em.work();

        }

    }

}

 

posted @ 2021-10-07 20:01  谦寻  阅读(60)  评论(0编辑  收藏  举报