简体   繁体   English

抽象类不能用c#封装?

[英]Abstract class cannot be sealed in c#?

I read somewhere 我在某处读到了

"Abstract and Sealed modifiers are equivalent to a class which is static" “抽象和密封修饰符相当于一个静态的类”

I also found that 我也发现了

"When you declare a static class, internally the compiler marks the class abstract and sealed, and creates a private constructor in the IL code" “当你声明一个静态类时,编译器在内部标记类抽象并密封,并在IL代码中创建一个私有构造函数”

so, I decided to do this: 所以,我决定这样做:

static class A
{
    public static void test()
    {
        Console.WriteLine("test");
    }
}

Now, the class "A" cannot be inherited nor instantiated. 现在,类“A”不能被继承或实例化。

So, let us write a class B using abstract to prevent instantiation and using sealed to prevent inheritance. 因此,让我们使用abstract编写一个class B来防止实例化并使用sealed来防止继承。

But, this approach fails. 但是,这种方法失败了。

which should be equivalent to 这相当于

public abstract sealed class B
{
    private B()
    {

    }
    public void test()
    {
        Console.WriteLine("test");
    }
}

But I recieve an error stating "error CS0418: B': an abstract class cannot be sealed or static"` . But I recieve an error stating "error CS0418: B':抽象类不能被密封或静态”。 Any ideas why this is not possible ? 任何想法为什么这是不可能的?

Thanks in advance for your answers. 提前感谢您的回答。

Having checked the IL of the System.Directory class (which is static), it is declared in IL as: 检查了System.Directory类的IL(它是静态的)后,它在IL中声明为:

.class public auto ansi abstract sealed beforefieldinit System.IO.Directory
extends System.Object
{
    ...

Further, this article ( http://msdn.microsoft.com/en-us/library/ms229038.aspx ) suggests that the CLR handles static classes as abstract sealed classes to support languages that do not support directly delcaring static classes (eg C++). 此外,本文( http://msdn.microsoft.com/en-us/library/ms229038.aspx )建议CLR将静态类作为抽象密封类处理,以支持不支持直接删除静态类的语言(例如C ++) )。

Thus in conclusion, static classes in C# are syntactic sugar for sealed abstract classes with private constructors. 因此,总之,C#中的静态类是具有私有构造函数的密封抽象类的语法糖。 I for one am glad of that as "static" is a lot easier to write and a lot easier to get right. 我很高兴,因为“静态”更容易编写,更容易正确。

By definition a sealed class enables you to prevent the inheritance of a class or certain class members that were previously marked virtual. 根据定义,使用sealed类可以防止继承先前标记为虚拟的类或某些类成员。

Abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class. Abstract关键字使您可以创建不完整的类和类成员,并且必须在派生类中实现。 (Source: http://msdn.microsoft.com/en-us/library/ms173150.aspx ) (来源: http//msdn.microsoft.com/en-us/library/ms173150.aspx

This would imply that any class marked abstract would not be able to be sealed, since you wouldn't be able to derive it anywhere. 这意味着任何标记为摘要的类都不能被密封,因为你无法在任何地方获得它。

The code you mentioned doesn't make any sense. 你提到的代码没有任何意义。

All answers somehow take the technical point of view. 所有答案都以某种方式采用技术观点。 Such as: the class can't be "must inherit" and "can't inherit" at the same time. 如:类不能同时“必须继承”和“不能继承”。 But I think that is not the main reason, as clearly, the "static" is just that. 但我认为这不是主要原因,显然,“静态”就是这样。 I think David Amo's answer has touched the real answer a bit, by stating: "it is a lot easier to get right". 我认为David Amo的回答有点触及了真正的答案,他说:“要做得更好更容易”。

I am convinced that Anders Hejlsberg's idea when designing C# was to eliminate ambiguity and thus decrease a chance for error. 我确信Anders Hejlsberg在设计C#时的想法是消除歧义,从而减少出错的机会。 That's why "virtual" goes with "override" (override has to be explicit, not implicit as in Java). 这就是“虚拟”与“覆盖”相关的原因(覆盖必须是显式的,而不是像Java中那样隐式)。 And in this case, "abstract"+"sealed" would be the same as "static". 在这种情况下,“抽象”+“密封”将与“静态”相同。 Two ways of defining the same principle. 两种定义相同原理的方法。 This is: - more error prone (imagine you have abstract somewhere and put sealed there accidently without noticing, onw compiler prevents that) - more difficult to work with (imagine you want to search for all static classes in your project) 这是: - 更容易出错(假设你有一个抽象的东西并且意外地密封在那里而没有注意到,onw编译器阻止了这一点) - 更难以使用(想象你想在项目中搜索所有静态类)

So my point is, this entire design leads the developers the right way of doing things. 所以我的观点是,整个设计引导开发人员正确的做事方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM