zoukankan      html  css  js  c++  java
  • C#设计编写一个控制台应用程序,练习类的继承

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

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

    覆盖Work 方法。

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

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

    using System;
    
    namespace sy2_1
    {
        abstract class People //抽象类 People
        {
            public string name;//姓名
            public int age;//年龄
            public abstract void work();//work方法
        }
    
        class Student : People
        {
            public string school;//学校
            public override void work()
            {
                Console.WriteLine("学生去上学");
            }
        }
    
        class Employer : People 
        {
            public string workplace;//工作单位
            public override void work()
            {
                Console.WriteLine("职工在办公");
            }
        }
    
        class Program 
        {
            static void Main(string[] args)
            {
                Student student = new Student();
                Console.WriteLine("请输入学生姓名:");
                student.name = Console.ReadLine();
                Console.WriteLine("请输入学生年龄:");
                student.age = int.Parse(Console.ReadLine());
                Console.WriteLine("请输入学生学校:");
                student.school = Console.ReadLine();
    
                Employer employer = new Employer();
                Console.WriteLine("请输入职工姓名:");
                employer.name = Console.ReadLine();
                Console.WriteLine("请输入职工年龄:");
                employer.age = int.Parse(Console.ReadLine());
                Console.WriteLine("请输入职工工作单位:");
                employer.workplace = Console.ReadLine();
    
                student.work();
                Console.WriteLine("学生姓名:{0},学生年龄{1},学生学校{2}"
                    ,student.name,student.age,student.school);
                employer.work();
                Console.WriteLine("职工姓名:{0},职工年龄{1},职工工作单位{2}"
                    , employer.name, employer.age, employer.workplace);
            }
        }
    }

    结果:

  • 相关阅读:
    HashMap 和 Hashtable 的区别
    使用 final 关键字修饰一个变量时,是引用不能变,还是引用的对象不能变?
    short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?
    io--文件内容的复制
    heap与stack的区别
    序列化与反序列化
    实现反转的方法(reverse)
    final, finally, finalize 的区别
    request.getSession()、reqeust.getSession(false)和request.getSession(true)
    如何优化网页的加载速度
  • 原文地址:https://www.cnblogs.com/Arisf/p/15542914.html
Copyright © 2011-2022 走看看