如果在 C# 中是密封的、继承的或抽象的,如何知道是否可以实例化一个类或接口?

我很难知道可以在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();

    }

}

那么,我们可以从上面使用哪些?我知道这是一个愚蠢的问题,但这是我们在大学考试中实际得到的结果。


翻过高山走不出你
浏览 158回答 3
3回答

隔江千里

您的大多数类声明都无法编译。只有B,D和Gcompile的声明。interface A {public void Method();} // "public" cannot be used on interface membersclass 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 implementedclass F:A {public void Method();} // method does not have a bodyclass G:C {};对于 中的语句Main,它们中的大多数也不编译:A a = new A(); // cannot instantiate interface AB b = new B(); // OK because B is a normal classA ab = new B(); // B can be instantiated for aforementioned reasons, but cannot be                // assigned to A because they are unrelated typesB 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 CD 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 instantiationE e = new E(); // OK, E is a normal classF af = new A(); // cannot instantiate interface AA fa = new F(); // F is a normal class, and is assignable to A because F implements AG g = new G(); // OK, G is a normal class一般模式:抽象类和接口不能被实例化该sealed关键字无关的实例化只有私有构造函数的类不能被实例化如果满足以下条件,则可以将 T1 类型的表达式分配给 T2 类型的变量:T1和T2是同一类型,或;T1 继承 T2,或者;T1 实现 T2,或者;T1 可隐式转换为 T2

慕的地6264312

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    }}

MMMHUHU

您不能新建接口或抽象类,因此会导致错误。接口只是合约。他们不是班级。你不能实例化它们。抽象类只能被继承。它们不能自己创建。密封类只是意味着它们不能被继承。您仍然可以实例化它们。
打开App,查看更多内容
随时随地看视频慕课网APP