using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace override_new_ex
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_Compute_Click(object sender, EventArgs e)
{
string Msg = "";
if (combo_Cars.Text != "")
{
if (combo_Cars.SelectedIndex == 0)
{
SportCar Eclipse = new SportCar();
Msg = Msg + "車名=" + combo_Cars.Text + "/r/n";
Msg = Msg + "車門數=" + Eclipse.CarDoors.ToString() + "/r/n";
Msg = Msg + "馬力=" + Eclipse.Horsepower.ToString() + "/r/n";
Msg = Msg + "極速=" + Eclipse.Maxspeed.ToString();
MessageBox.Show(this, Msg, "new方法");
}
else
{
Convertible Peugeot206 = new Convertible();
Msg = Msg + "車名=" + combo_Cars.Text + "/r/n";
Msg = Msg + "馬力=" + Peugeot206.Horsepower.ToString() + "/r/n";
Msg = Msg + "極速=" + Peugeot206.Maxspeed.ToString() + "/r/n";
Msg = Msg + "引擎技術=" + Peugeot206.EngineTechnology(false) + "/r/n";
MessageBox.Show(this, Msg, "override方法");
}
}
else
{
MessageBox.Show(this, "請選擇一輛車", "訊息");
}
}
}
}
public class Car //汽車
{
public int AluminumWheel; //鋁圈
public int CarDoors { get { return 4; } } //車門
public virtual int Horsepower { get { return 100; } } //馬力
public virtual int Maxspeed { get { return 150; } } //極速
//供油方式
public string FuelSystem()
{ return "One-Port Fuel Injected"; } //單點噴射
//引擎技術
public virtual string EngineTechnology(bool IsTurbo)
{
if (IsTurbo == true)
return "Turbo";//渦輪增壓
else
return "Natural Aspirate"; //自然進氣
}
}
public class SportCar : Car //跑車繼承了汽車
{
public new int CarDoors { get { return 2; } } //車門
public new int Horsepower { get { return 205; } } //馬力
public new int Maxspeed { get { return 260; } } //極速
//供油方式
public new string FuelSystem()
{ return "Multi-Port Fuel Injected"; } //多點噴射
}
public class Convertible : Car //敞篷車繼承了汽車
{
public override int Horsepower { get { return 110; } } //馬力
public override int Maxspeed { get { return 180; } } //極速
public override string EngineTechnology(bool IsTurbo)
{
if (IsTurbo == true)
return "Turbo";//渦輪增壓
else
return "Natural Aspirate"; //自然進氣
}
}