C# 程式設計習題 -- 人、學生與老師
基礎篇C# 簡介開發環境變數與運算流程控制陣列函數物件例外處理函式庫篇檔案處理資料結構正規表達式Thread應用篇視窗程式媒體影音網路程式遊戲程式手機程式資料庫雲端運算特殊功能委派擴展方法序列化LinQWPF網路資源教學影片投影片教學文章軟體下載考題解答101習題 |
請定義「人、學生與老師」等三個類別,其中的人有「姓名、性別、年齡」三個欄位,學生另外有「學號、年級」等兩個欄位,老師另外有「職等」的欄位,所有物件都有 print() 函數,可以將該物件的所有欄位印出。 請建立一個具有 3 個學生與兩個老師的陣列,利用多型機制,呼叫 print 函數以便印出這 5 個人的基本資料,如下所示。
解答
|
page revision: 4, last edited: 26 Oct 2010 00:54
http://mirokokeiorl.wikidot.com/windowhomework
http://ogdoaddeity.wikidot.com/homework1
陳煜珊109810656
http://s109810656.wikidot.com/system
using System;
using System.Collections.Generic;
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
{
String id;
int degree;
public Student(String name, String sex, int age, String id, int degree)
: base(name, sex, age)
{
this.id = id;
this.degree = degree;
}
public override Person print()
{
Console.Write("學生 — ");
base.print();
Console.Write(" 學號:" + id + " 年級:" + degree);
return this;
}
}
class teacher : Person
{
String Lv;
public teacher(String name, String sex, int age, String Lv)
: base(name, sex, age)
{
this.Lv = Lv;
}
public override Person print()
{
Console.Write("老師 — ");
base.print();
Console.Write(" 職等:" + Lv);
return this;
}
}
}
http://redzero.wikidot.com/y
using System;
using System.Collections.Generic;
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
{
String id;
int degree;
public Student(String name, String sex, int age, String id, int degree)
: base(name, sex, age)
{
this.id = id;
this.degree = degree;
}
public override Person print()
{
Console.Write("學生 — ");
base.print();
Console.Write(" 學號:" + id + " 年級:" + degree);
return this;
}
}
class Teacher : 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;
}
}
}
using System;
using System.Collections.Generic;
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
{
String id;
int degree;
public Student(String name, String sex, int age, String id, int degree)
: base(name, sex, age)
{
this.id = id;
this.degree = degree;
}
public override Person print()
{
Console.Write("學生 — ");
base.print();
Console.Write(" 學號:" + id + " 年級:" + degree);
return this;
}
}
class Teacher : 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;
}
}
}
using System;
using System.Collections.Generic;
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
{
String id;
int degree;
public Student(String name, String sex, int age, String id, int degree)
: base(name, sex, age)
{
this.id = id;
this.degree = degree;
}
public override Person print()
{
Console.Write("學生 — ");
base.print();
Console.Write(" 學號:" + id + " 年級:" + degree);
return this;
}
}
class Teacher : 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;
}
}
}
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("潘勝利", "男", 20, "教授");
Teacher t2 = new Teacher("小朋友", "女", 18, "實習老師");
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
{
String id;
int degree;
public Student(String name, String sex, int age, String id, int degree)
: base(name, sex, age)
{
this.id = id;
this.degree = degree;
}
public override Person print()
{
Console.Write("學生 — ");
base.print();
Console.Write(" 學號:" + id + " 年級:" + degree);
return this;
}
}
class Teacher : 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;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Person
{
/*
學生 — 姓名:王小明,性別:男,年齡: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
{
String id;
int degree;
public Student(String name, String sex, int age, String id, int degree)
: base(name, sex, age)
{
this.id = id;
this.degree = degree;
}
public override Person print()
{
Console.Write("學生 — ");
base.print();
Console.Write(" 學號:" + id + " 年級:" + degree);
return this;
}
}
class Teacher : 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;
}
}
}
Post preview:
Close preview