.NET中静态和密封类之间的区别 [英] difference between static and sealed class in .NET

查看:102
本文介绍了.NET中静态和密封类之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨亲爱的朋友,



我有一个问题



i有学习班级静态或密封上课



1)两个班都不能继承

2)我们不能创建对象

3 )我们直接使用名字



但是这两个班级有什么不同



i想想这一切回答更新鲜的水平。

但我想作为体验人回答





提前付款

Anil kumar

解决方案

他们完全不同。



A 密封 class不能被继承:这是它与任何其他类型的类别的唯一区别。



A static 类无法实例化。你不能在没有编译器抱怨的情况下随时编写 new MyStaticClass()。它从来没有这个引用,只能通过类名访问字段,属性和方法: MyStaticClass.MyMethod()另外,你不能从静态类派生,静态类不能从 object

< blockquote>一个静态类只能包含静态方法,属性和字段,你在1),2)和3)中写的内容适用。

密封类是一个普通的类,你可以创建一个实例但是你不能继承它。您在2)和3)中所写的内容不适用于密封类,严格的密封类可以使用静态方法,因此您可以争论3)适用。



For更深入的观点见静态类和静态类成员 [ ^ ],密封(C#参考) [ ^ ]和摘要和密封的班级和班级成员 [ ^ ]


密封类:
1)可以创建实例,但不能继承
2)可以包含静态成员和非静态成员。

静态类:
1)既不能创建实例也不能继承它们
2)只能有静态成员。


Hi Dear friend ,

I have one question

i have study both class static or sealed class

1)Both class can't be inheritance
2)we can not create object
3) we directly use by name

but what is the differences in these two class

i think all this answer as fresher level .
but i want answer as a experience person


Thanks in advance
Anil kumar

解决方案

They are completely different.

A sealed class cannot be inherited: that is it's only difference from any other type of class.

A static class cannot be instantiated. You cannot, at any time write new MyStaticClass() without the compiler complaining. It never has a this reference, and the fields, properties and methods can only ever be accessed via the class name: MyStaticClass.MyMethod() In addition, you cannot derive from a static class, not can a static class derive from any class other than object


A static class can only contain static Methods, Properties and Fields and what you wrote in 1), 2) and 3) applies.
A sealed class is a normal class which you can make an instance of, but you cannot inherit from it. what you wrote in 2) and 3) does not apply to sealed classes, tough a sealed class can have static methods and thus you can argue 3) applies.

For a more indepth view see Static Classes and Static Class Members[^], sealed (C# Reference)[^] and Abstract and Sealed Classes and Class Members[^]


Sealed classes: 
1)Can create instances, but cannot inherit
2)Can contain static as well as nonstatic members.

Static classes:
1)Can neither create their instances, nor inherit them
2)Can have static members only.


这篇关于.NET中静态和密封类之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆