Ex

題目

請定義「人、學生與老師」等三個類別,其中的人有「姓名、性別、年齡」三個欄位,學生另外有「學號、年級」等兩個欄位,老師另外有「職等」的欄位,所有物件都有 print() 函數,可以將該物件的所有欄位印出。

請建立一個具有 3 個學生與兩個老師的陣列,利用多型機制,呼叫 print 函數以便印出這 5 個人的基本資料,如下所示。

 學生 — 姓名:王小明,性別:男,年齡:20,學號:R773122456,年級:一年級
 學生 — 姓名:李小華,性別:女,年齡:19,學號:R773122432,年級:一年級
 教師 — 姓名:陳福氣,性別:男,年齡:40,職等:教授
 學生 — 姓名:黃大虎,性別:男,年齡:22,學號:R773122721,年級:四年級
 教師 — 姓名:李美女,性別:女,年齡:35,職等:助理教授

程式碼

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

namespace Person
{
    /* 題目
     * 請定義「人、學生與老師」等三個類別,其中的人有「姓名、性別、年齡」三個欄位,學生另外有「學號、年級」
     * 等兩個欄位,老師另外有「職等」的欄位,所有物件都有 print() 函數,可以將該物件的所有欄位印出。
     * 請建立一個具有 3 個學生與兩個老師的陣列,利用多型機制,呼叫 print 函數以便印出這 5 個人的基本資料,
     * 如下所示。

     * 學生 --  姓名:王小明,性別:男,年齡:20,學號:R773122456,年級:一年級
     * 學生 --  姓名:李小華,性別:女,年齡:19,學號:R773122432,年級:一年級
     * 教師 --  姓名:陳福氣,性別:男,年齡:40,職等:教授
     * 學生 --  姓名:黃大虎,性別:男,年齡:22,學號:R773122721,年級:四年級
     * 教師 --  姓名:李美女,性別:女,年齡:35,職等:助理教授
     */
    class Program
    {
        static void Main(string[] args)
        {
            student s1 = new student("王小明", "男", 20, "R773122456", 1);
            student s2 = new student("李小華", "女", 19, "R773122432", 1);
            student s3 = new student("黃大虎", "男", 22, "R773122721", 4);
            teacher t1 = new teacher("陳福氣", "男", 40,"教授");
            teacher t2 = new teacher("李美女", "女", 35,"助理教授");

            person[] list = { s1, s2, t1, s3, t2 };//宣告陣列
            foreach (person p in list)
            {
                p.print();
                Console.WriteLine();//每行執行完跳下一行
            }

        }
    }

    class person
    {
        string name;
        string sex;
        int age;

        public person(string name, string sex, int age)
        {
            this.name = name;
            this.sex = sex;
            this.age = age;
        }

        public virtual person print()
        {
            Console.Write("姓名:" + name + ",性別:" + sex + ",年齡:" + age);
            return this;
        }

    }

    class student : person //繼承person 
    {
        string id;
        int degree;

        public student(string name, string sex, int age, string id, int degree)
            : base(name, sex, age)//結構函數,base 呼叫副類別的建構函數
        {
            this.id = id;
            this.degree=degree;
        }

        public override person print()
        {
            Console.Write("學生 -- ");
            base.print();
            Console.Write(",學號:" + id + ",年級" + degree);
            return this;
        }
    }

    class teacher : person //繼承person
    {
        string level;

        public teacher(string name, string sex, int age, string level)
            : base(name, sex, age)
        {
            this.level = level;
        }

        public override person print()
        {
            Console.Write("老師 -- ");
            base.print();
            Console.Write(",職等:" + level);
            return this;
        }
    }

}

執行結果
person01.bmp
person.JPG

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License