ブログエントリ by 97213529 郭益銘

2009年 01月 11日(日曜日) 21:10 - 97213529 郭益銘 の投稿
世界中の誰でも閲覧可
class Employee
{
private string info; //Private屬性,其他繼承的類別無法改變它的值
public string id, name, sex, position; //其他繼承的類別可以使用這些屬性
public int age, days, salary; //其他繼承的類別可以使用這些屬性

public void ShowPrivateInfo() //透過繼承類別呼叫ShowPrivateInfo()
{
info = "This is a program about class inheritance.";

Console.WriteLine(info);
}

public void ShowBaseInfo() //透過繼承類別呼叫ShowBaseInfo()
{
Console.WriteLine(position + "的基本資訊:") ;
Console.WriteLine("編號:[" + id + "]");
Console.WriteLine("姓名:[" + name + "]");
Console.WriteLine("性別:[" + sex + "]");
Console.WriteLine("年齡:[" + age+ "]");
Console.WriteLine("本月工作天數:[" + days + "]");
}
}

//-----------------------------------------------------------------------------------------

class Manager : Employee //Manager繼承Employee,但薪水算法不同
{
public void CountSalary() //Manager的薪水計算方式:將工作天數帶入薪水計算公式
{
salary = 60000 + days * 1000;

Console.WriteLine(position + "的本月薪水:[" + salary + "]");
}
}

class Normal : Employee //Normal繼承Employee,但薪水算法不同
{
public void CountSalary() //Normal的薪水計算方式:將工作天數帶入薪水計算公式
{
salary = days * 1500;

Console.WriteLine(position + "的本月薪水:[" + salary + "]");
}
}

class HourCount : Employee //HourCount繼承Employee,但薪水算法不同
{
public void CountSalary() //HourCount的薪水計算方式:將工作天數帶入薪水計算公式
{
salary = days * 100 * 8 ;

Console.WriteLine(position + "的本月薪水:[" + salary + "]");
}
}

//-----------------------------------------------------------------------------------------

class Program
{
static void Main(string[] args)
{
string choice;
bool exit = false;

Console.Write("請選擇要輸的的員工資訊:(a)經理 (b)一般員工 (c)時薪人員 (d)離開程式:");
choice = Console.ReadLine();

while (exit == false)
{
if (choice == "a")
{
Manager mm = new Manager(); //宣告一個Manager類別

mm.position = "經理";
mm.ShowPrivateInfo();

//輸入經理的各項資訊
Console.WriteLine("請輸入" + mm.position + "訊息");
Console.Write("編號:");
mm.id = Console.ReadLine();
Console.Write("姓名:");
mm.name = Console.ReadLine();
Console.Write("性別:");
mm.sex = Console.ReadLine();
Console.Write("年齡:");
mm.age = Int32.Parse(Console.ReadLine());
Console.Write("本月工作天數:");
mm.days = Int32.Parse(Console.ReadLine());

Console.WriteLine("-----------------------------------------------------");

//顯示經理的基本訊息與該月薪水
mm.ShowBaseInfo();
mm.CountSalary();
}
else if (choice == "b")
{
Normal nn = new Normal();

nn.position = "一般員工";
nn.ShowPrivateInfo();

//輸入一般員工的各項資訊
Console.WriteLine("請輸入" + nn.position + "訊息");
Console.Write("編號:");
nn.id = Console.ReadLine();
Console.Write("姓名:");
nn.name = Console.ReadLine();
Console.Write("性別:");
nn.sex = Console.ReadLine();
Console.Write("年齡:");
nn.age = Int32.Parse(Console.ReadLine());
Console.Write("本月工作天數:");
nn.days = Int32.Parse(Console.ReadLine());

Console.WriteLine("-----------------------------------------------------");

//顯示一般員工的基本訊息與該月薪水
nn.ShowBaseInfo();
nn.CountSalary();
}
else if (choice == "c")
{
HourCount hh = new HourCount();

hh.position = "時薪人員";
hh.ShowPrivateInfo();

//輸入時薪人員的各項資訊
Console.WriteLine("請輸入" + hh.position + "訊息");
Console.Write("編號:");
hh.id = Console.ReadLine();
Console.Write("姓名:");
hh.name = Console.ReadLine();
Console.Write("性別:");
hh.sex = Console.ReadLine();
Console.Write("年齡:");
hh.age = Int32.Parse(Console.ReadLine());
Console.Write("本月工作天數:");
hh.days = Int32.Parse(Console.ReadLine());

Console.WriteLine("-----------------------------------------------------");

//顯示時薪人員的基本訊息與該月薪水
hh.ShowBaseInfo();
hh.CountSalary();
}
else if (choice == "d") //按 d 離開程式
{
exit = true;
break;
}
else //輸入錯誤時顯示錯誤訊息
{
Console.WriteLine("-----------------------------------------------------");
Console.WriteLine("請輸入正確的選項或是按(d)離開程式!!");
}

Console.WriteLine("-----------------------------------------------------");
Console.Write("請選擇要輸的的員工資訊:(a)經理 (b)一般員工 (c)時薪人員 (d)離開程式:");
choice = Console.ReadLine();
}
}
}