C#設計編寫一個控制台應用程序


設計編寫一個控制台應用程序,練習類的繼承。

(1) 編寫一個抽象類 People,具有”姓名”,”年齡”字段,”姓名”屬性,Work 方法。

(2) 由抽象類 People 派生出學生類 Student 和職工類 Employer,繼承 People 類,並覆蓋Work 方法。

(3) 派生類 Student 增加”學校”字段,派生類 Employer 增加”工作單位”字段。

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

代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lab06_1
{
    abstract class People   //抽象類:people
    {
        public String name; //姓名
        public String Name { get; set; }//姓名屬性
        public int age;//年齡
        public abstract void work();//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 Program
    {
        static void Main(string[] args)
        {
            Student st=new Student();
            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.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();
Console.Write("\n請按任意鍵繼續...");
            Console.ReadKey();
        }
    }
   
}

運行結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM