亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果在 C# 中是密封的、繼承的或抽象的,如何知道是否可以實例化一個類或接口?

如果在 C# 中是密封的、繼承的或抽象的,如何知道是否可以實例化一個類或接口?

C#
翻過高山走不出你 2022-01-09 14:59:19
我很難知道可以在Main方法中實例化以下哪些類/接口(哪個類/接口完全可以)。對于某些類和接口,代碼如下所示:interface A {public void Method();}class B {public static int b;}abstract class C:B {public void Method1();}sealed class D:B {} ;class E:A {};class F:A {public void Method();}class G:C {};然后稍后,我們在另一個類中有 Main 方法,就像這樣......class Program {    static void Main(string[] args)     {        A a = new A();        B b = new B();        A ab = new B();        B ba = new A();        C c = new C();        D d = new D();        E e = new E();        F af = new A();        A fa = new F();        G g = new G();    }}那么,我們可以從上面使用哪些?我知道這是一個愚蠢的問題,但這是我們在大學考試中實際得到的結果。
查看完整描述

3 回答

?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

您的大多數類聲明都無法編譯。只有B,D和Gcompile的聲明。


interface A {public void Method();} // "public" cannot be used on interface members

class B {public static int b;}

abstract class C:B {public void Method1();} // method without body should be marked as "abstract"

sealed class D:B {} ;

class E:A {}; // interface methods not implemented

class F:A {public void Method();} // method does not have a body

class G:C {};

對于 中的語句Main,它們中的大多數也不編譯:


A a = new A(); // cannot instantiate interface A

B b = new B(); // OK because B is a normal class

A ab = new B(); // B can be instantiated for aforementioned reasons, but cannot be

                // assigned to A because they are unrelated types

B ba = new A(); // cannot instantiate interface A. A also cannot be assigned to

                // B because they are unrelated.

C c = new C(); // cannot instantiate abstract class C

D d = new D(); // OK, D is a normal class. It is sealed, but that just means no 

               // class can derive from it, nothing to do with instantiation

E e = new E(); // OK, E is a normal class

F af = new A(); // cannot instantiate interface A

A fa = new F(); // F is a normal class, and is assignable to A because F implements A

G g = new G(); // OK, G is a normal class

一般模式:

  • 抽象類和接口不能被實例化

  • sealed關鍵字無關的實例化

  • 只有私有構造函數的類不能被實例化

  • 如果滿足以下條件,則可以將 T1 類型的表達式分配給 T2 類型的變量:

    • T1和T2是同一類型,或;

    • T1 繼承 T2,或者;

    • T1 實現 T2,或者;

    • T1 可隱式轉換為 T2


查看完整回答
反對 回復 2022-01-09
?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

abstract不能實例化一個類。

所述sealed改性劑防止被繼承的類和abstract改性劑需要被繼承的類。

來源:https : //docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/abstract


接口不能直接實例化。

來源:https : //docs.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/


這意味著:


class Program 

{

    static void Main(string[] args) 

    {

        A a = new A();  // ERROR: cannot instantiate interface

        B b = new B();  // OK

        A ab = new B(); // ERROR: class B doesn't implement interface A

        B ba = new A(); // ERROR: cannot instantiate interface

        C c = new C();  // ERROR: cannot instantiate abstract class

        D d = new D();  // OK

        E e = new E();  // OK

        F af = new A(); // ERROR: cannot instantiate interface

        A fa = new F(); // OK:    class F does implement interface A

        G g = new G();  // OK

    }

}


查看完整回答
反對 回復 2022-01-09
?
MMMHUHU

TA貢獻1834條經驗 獲得超8個贊

您不能新建接口或抽象類,因此會導致錯誤。

接口只是合約。他們不是班級。你不能實例化它們。

抽象類只能被繼承。它們不能自己創建。

密封類只是意味著它們不能被繼承。您仍然可以實例化它們。


查看完整回答
反對 回復 2022-01-09
  • 3 回答
  • 0 關注
  • 154 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號