基類與介面混合繼承的聲明問題

來源:互聯網
上載者:User

1. 問題初現

今天,查看《介面繼承的聲明問題》一文的反饋,發現Ninputer留下這樣一道題:

如果有

class A : Interface1

那麼

class B : A, Inteface1

和

class B : A

會出現什麼不同的情況呢。編譯器在IL層級是用什麼手段實現這個功能的呢?

2. 探索問題 & 理解問題

解決問題的過程既是一個探索的過程也是一個推理論證的過程。OK,下面我嘗試用反證法來探索這個問題。

首先,我假設問題中B類的兩種繼承方式有著一樣的效果,並試著尋找它們的不一樣。為了瞭解這兩種方式的效果,我把上面代碼補充完整:

interface IC { }
class A : IC { }
class B1 : A { }
class B2 : A, IC { }
class Program
{
  static void Main()
  {
    A a = new A();
    B1 b1 = new B1();
    B2 b2 = new B2();
    Console.WriteLine(a is IC);
    Console.WriteLine(b1 is A);
    Console.WriteLine(b1 is IC);
    Console.WriteLine(b2 is A);
    Console.WriteLine(b2 is IC);
  }
}

代碼啟動並執行結果是:

True
True
True
True
True

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.