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

How to know if you can instantiate a class or interface if it is sealed, inherited or abstract in C#?

我很难知道我可以在 Main 方法中实例化以下哪些 classes/接口(哪个 class/接口完全可以)。对于一些 classes 和接口,代码是这样的:

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

然后,我们在另一个 Class 中有 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();
    }
}

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

您不能新建接口或抽象 类,因此这些会导致错误。

接口只是契约。他们不是 类。你不能实例化它们。

摘要类只能继承。它们不能自己创建。

Sealed 类就是说不能继承。您仍然可以实例化它们。

  • An abstract class cannot be instantiated.
  • The sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.

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

  • An interface can't be instantiated directly.

来源: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
    }
}

您的大部分 class 声明都无法编译。只有 BDG 的声明可以编译。

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

一般模式:

  • 抽象classes和接口无法实例化
  • sealed关键字与实例化无关
  • class只有私有构造函数的es无法实例化
  • 如果满足以下条件,则可以将类型 T1 的表达式分配给类型 T2 的变量:
    • T1和T2是同一类型,或者;
    • T1继承T2,或者;
    • T1 实现 T2,或者;
    • T1 可隐式转换为 T2