有兩種方式:
1.子類別透過new來取代父類別的成員
2.父類別中宣告Virtual成員,然後在子類別使用override來覆寫父類別的成員
class father
{
  public int abc;
  public virtual int cde; //這是不行的,override不能針對變數
  public string methodA();
  public virtual int propertyA{ get {return 0;} }
  public virtual string methodB();
}
class son : father
{
  public new int abc;
  public new string methodA();
  public new int propertyA{ get { return 10;} }  //new可以不管父類別是否有設定Virtual
  public new string methodB();
}
class daughter : father

  public override int propertyA{ get {return 0;} } //override僅適用父類別中有設定Virtual的方法、屬性、事件
  public override string methodB();
}
以下是範例

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"; //自然進氣
        }
    }

arrow
arrow
    全站熱搜

    羅 朝淇 發表在 痞客邦 留言(0) 人氣()