abstract virtual override 虛基類

C# 允許您使用關鍵字 abstract 創建抽象類,用於提供接口的部分類的實現。當一個派生類繼承自該抽象類時,實現即完成。抽象類包含抽象方法,抽象方法可被派生類實現。派生類具有更專業的功能。

請注意,下面是有關抽象類的一些規則:

  • 您不能創建一個抽象類的實例。
  • 您不能在一個抽象類外部聲明一個抽象方法。
  • 通過在類定義前面放置關鍵字 sealed,可以將類聲明爲密封類。當一個類被聲明爲 sealed 時,它不能被繼承。抽象類不能被聲明爲 sealed。
using System;
namespace PolymorphismApplication
{
   abstract class Shape
   {
      public abstract int area();
   }
   class Rectangle:  Shape
   {
      private int length;
      private int width;
      public Rectangle( int a=0, int b=0)
      {
         length = a;
         width = b;
      }
      public override int area ()
      { 
         Console.WriteLine("Rectangle 類的面積:");
         return (width * length); 
      }
   }

   class RectangleTester
   {
      static void Main(string[] args)
      {
         Rectangle r = new Rectangle(10, 7);
         double a = r.area();
         Console.WriteLine("面積: {0}",a);
         Console.ReadKey();
      }
   }
}
當上面的代碼被編譯和執行時,它會產生下列結果:

Rectangle 類的面積:
面積: 70



當有一個定義在類中的函數需要在繼承類中實現時,可以使用虛方法。虛方法是使用關鍵字 virtual 聲明的。虛方法可以在不同的繼承類中有不同的實現。對虛方法的調用是在運行時發生的。

動態多態性是通過 抽象類 和 虛方法 實現的。

virtual 關鍵字允許在派生類中重寫這些對象。默認情況下,方法是非虛擬的,不可以重寫非虛方法,virtual關鍵字不可以與static、abstract、private、override一起使用。virtual關鍵字又是和override緊密不可分的,如果要實現virtual方法就必須要使用override或new關鍵字(上文已經指出new和override產生的機理不同)。

using System;
namespace PolymorphismApplication
{
   class Shape 
   {
      protected int width, height;
      public Shape( int a=0, int b=0)
      {
         width = a;
         height = b;
      }
      public virtual int area()
      {
         Console.WriteLine("父類的面積:");
         return 0;
      }
   }
   class Rectangle: Shape
   {
      public Rectangle( int a=0, int b=0): base(a, b)
      {

      }
      public override int area ()
      {
         Console.WriteLine("Rectangle 類的面積:");
         return (width * height); 
      }
   }
   class Triangle: Shape
   {
      public Triangle(int a = 0, int b = 0): base(a, b)
      {
      
      }
      public override int area()
      {
         Console.WriteLine("Triangle 類的面積:");
         return (width * height / 2); 
      }
   }
   class Caller
   {
      public void CallArea(Shape sh)
      {
         int a;
         a = sh.area();
         Console.WriteLine("面積: {0}", a);
      }
   }  
   class Tester
   {
      
      static void Main(string[] args)
      {
         Caller c = new Caller();
         Rectangle r = new Rectangle(10, 7);
         Triangle t = new Triangle(10, 5);
         c.CallArea(r);
         c.CallArea(t);
         Console.ReadKey();
      }
   }
}

當上面的代碼被編譯和執行時,它會產生下列結果:

Rectangle 類的面積:
面積:70
Triangle 類的面積:
面積:25





虛基類


#include <iostream>
using namespace std;
class B0// 聲明爲基類B0
{
    int nv;//默認爲私有成員
public://外部接口
    B0(int n){ nv = n; cout << "Member of B0" << endl; }//B0類的構造函數
    void fun(){ cout << "fun of B0" << endl; }
};
class B1 :virtual public B0
{
    int nv1;
public:
    B1(int a) :B0(a){ cout << "Member of B1" << endl; }
};
class B2 :virtual public B0
{
    int nv2;
public:
    B2(int a) :B0(a){ cout << "Member of B2" << endl; }
};
class D1 :public B1, public B2
{
    int nvd;
public:
    D1(int a) :B0(a), B1(a), B2(a){ cout << "Member of D1" << endl; }// 此行的含義,參考下邊的 “使用注意5”
    void fund(){ cout << "fun of D1" << endl; }
};
int main(void)
{
    D1 d1(1);
    d1.fund();
    d1.fun();
    return 0;
}


執行結果:
Member of B0
Member of B1
Member of B2
Member of D1
fun of D1
fun of B0


發佈了31 篇原創文章 · 獲贊 2 · 訪問量 9214
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章