[英]Static and Sealed class differences
Is there any class that be implemented in static class?有没有在静态类中实现的类? means:
方法:
static class ABC : Anyclass
Is there any class which can be inherited in both sealed class and static class?是否有任何类可以在密封类和静态类中继承?
means:方法:
static class ABC : AClass {}
And和
sealed class ABC : AClass {}
May I be wrong in some extent?我可能在某种程度上是错的吗?
This may help you:这可能会帮助您:
+--------------+---+-------------------------+------------------+---------------------+
| 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 |
+--------------+---+-------------------------+------------------+---------------------+
In simple words简单来说
Static Class静态类
A class can be declared static, indicating that it contains only static members.
一个类可以声明为静态的,表明它只包含静态成员。 It is not possible to create instances of a static class using the new keyword.
无法使用 new 关键字创建静态类的实例。 Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.
加载包含类的程序或命名空间时,.NET Framework 公共语言运行时 (CLR) 会自动加载静态类。
Sealed Class密封类
A sealed class cannot be used as a base class.
密封类不能用作基类。 Sealed classes are primarily used to prevent derivation.
密封类主要用于防止派生。 Because they can never be used as a base class, some run-time optimizations can make calling sealed class members slightly faster.
因为它们永远不能用作基类,所以一些运行时优化可以使调用密封类成员的速度稍快一些。
You can let a sealed
class inherit from another class, but you cannot inherit from a sealed
class:您可以让
sealed
类从另一个类继承,但不能从sealed
类继承:
sealed class MySealedClass : BaseClass // is ok
class MyOtherClass : MySealedClass // won't compile
A static
class cannot inherit from other classes. static
类不能从其他类继承。
You can simply differentiate both of them as:您可以简单地将它们区分为:
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
Simple answer is a sealed class cannot be used as a base class .简单的回答是密封类不能用作基类。
I'm trying to show you sealed class is a derived class in the below code我试图向您展示密封类是下面代码中的派生类
public sealed class SealedClass : ClassBase
{
public override void Print()
{
base.Print();
}
}
and another sealed feature is only accessible with instance from it.(you can not inherit from it)另一个密封特性只能通过它的实例访问。(你不能从它继承)
class Program
{
static void Main(string[] args)
{
SealedClass objSeald = new SealedClass();
objSeald.Name = "Blah blah balh";
objSeald.Print();
}
}
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
}
1 - No, you can't implement a static class. 1 - 不,您不能实现静态类。
2 - No, you can't inherited from a static or sealed class 2 - 不,您不能从静态或密封类继承
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.