静态和密封类别差异

  1. 是否有任何类在静态类中实现? 意思是:

    static class ABC : Anyclass
    
  2. Is there any class which can be inherited in both sealed class and static class?
    means:

    static class ABC : AClass {}
    

    还有

    sealed class ABC : AClass {}
    

May I be wrong in some extent?

99244 次浏览

你可以让一个 sealed类继承另一个类,但是你不能继承 来自一个 sealed类:

sealed class MySealedClass : BaseClass // is ok
class MyOtherClass : MySealedClass     // won't compile

static类不能从其他类继承。

这可能对你有帮助:

+--------------+---+-------------------------+------------------+---------------------+
|  Class Type  |   | Can inherit from others | Can be inherited | Can be instantiated |
|--------------|---|-------------------------+------------------+---------------------+
| normal       | : |          YES            |        YES       |         YES         |
| abstract     | : |          YES            |        YES       |         NO          |
| sealed       | : |          YES            |        NO        |         YES         |
| static       | : |          NO             |        NO        |         NO          |
+--------------+---+-------------------------+------------------+---------------------+

简而言之

静态类别

类可以声明为静态的,表明它只包含 不可能创建静态成员的实例 静态类是自动加载的 当程式需要使用.NET Framework 通用语言运行库(CLR) 或包含该类的命名空间。

密封类

密封类不能用作基类 主要用于防止派生。因为它们永远不能被使用 作为基类,一些运行时优化可以使调用是密封的 类成员稍微快一点。

简单的答案是 密封类不能用作基类

在下面的代码中,我试图向你展示密封类是一个派生类

 public sealed class SealedClass : ClassBase
{
public override void Print()
{
base.Print();
}
}

另一个密封特性只能通过它的实例访问(您不能从它继承)

 class Program
{
static void Main(string[] args)
{
SealedClass objSeald = new SealedClass();
objSeald.Name = "Blah blah balh";
objSeald.Print();


}
}

不,你不能实现一个静态类。

2-不,您不能从静态或密封类继承

你可以简单地将它们区分为:

       Sealed Class       |        Static Class
--------------------------|-------------------------
it can inherit From other | it cannot inherit From other
classes but cannot be     | classes as well as cannot be
inherited                 | inherited
public class BaseClassDemo
{


}


//Note
//A static class can be used as a convenient container for sets of
//methods that just operate on input parameters and do not have to
//get or set any internal instance fields.
//The advantage of using a static class is that the compiler can
//check to make sure that no instance members are accidentally
//added. The compiler will guarantee that instances of this class
//cannot be created.




//Static class 'static type' cannot derive from type 'type'. Static
//classes must derive from object.
public static class StaticClassDemo : BaseClassDemo //Error






public static class StaticClassDemo
{
//Static class must have static members
public static void Ram()
{
throw new NotImplementedException();
}
}




//Sealed class can inherit from the base class.
public sealed class SealedClassDemo : BaseClassDemo
{


}


//We can't derive from sealed class.
public class derivedClass:SealedClassDemo //Error






public partial class RandomClass2
{
//we can't create instance of static class.
StaticClassDemo demo = new StaticClassDemo() //Error
}