密封课程的实时方案 [英] Real Time scenario for Sealed Class

查看:67
本文介绍了密封课程的实时方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

任何人都可以给我提供密封类的实时示例.
它不能被继承.那么拥有这样的课程有什么用呢?



在此先感谢
Froxy

Hi All

Can anyone provide me the real time example of sealed class.
It cannot be inherited. then what is the use to have class like this?



Thanks in advance
Froxy

推荐答案

您认为该类的唯一目的是要继承吗?正是这种方式,某些类将永远不会被使用!用于创建实例的类是什么?按照您的逻辑,某些带有后代的父类永远不会被实例化.你已经觉得荒谬了吗?

相反,大多数用于创建实例的类都是终端类,没有后代.怎么了?

现在,任何库的密封或非密封终端类之间在功能上都没有区别.我们可以轻松生活而无需密封",并且具有完全相同的功能. 密封"仅是一个简单的功能.您会看到,某些终端类旨在永久保留终端.它们用于创建实例,但是子类化就没有任何意义.因此,如果某些傻瓜"无法识别这种无用的情况(可以在文档中的额外句子中特别指出),则编译器错误将阻止此类开发人员执行此无用的步骤.只是一个额外的便利,节省了开发人员的时间.

(我在引号中使用傻瓜"表示这不是个人特征,而是对众所周知的技术术语防呆"的引用,意思是否则可能会犯错的人".) br/>
至于实时"示例-有成千上万个.没有任何示例,使用密封类不是合理的吗?在我看来,不理解它与不理解类"的概念几乎一样.

相反,更正确的问题是:您为什么认为这个或那个类需要子类化?例如,System.AppDomain http://msdn.microsoft.com/en-us/library /system.appdomain.aspx [ ^ ].子类化是通过扩展编程".那么,请告诉我们您将如何扩展应用程序域,为什么? .NET附带的许多其他类也是如此.每个开发人员也都创建类似的类.你呢? :-)

—SA
Do you think the only purpose of the class is to be inherited? It it was this way, some classes would never be used! What would be the classes used to create instances? Following you logic, some parent classes with descendants which are never instantiated. Do you feel the absurd of it already?

Just the opposite, most classes used to create instances, are terminal classes, having no descendants. What''s wrong with it?

Now, there is no functional difference between sealed or non-sealed terminal classes of any library. We could easily live without "sealed" and have the exact same functionality. "Sealed" is no more then a fool-proof feature. You see, some terminal classes are designed to remain terminal forever. They are used to create instances, but subclassing then just would not make no sense. So, if some "fool" fails to recognize such useless cases (which could be specially indicated in extra sentence in documentation), the compiler error will stop such developer from this useless step. Just an extra convenience and saved developer''s time.

(I take "fool" in quotation marks to indicate that this is not a personal characteristics, but a reference to a well-know technical term "fool-proof" meaning "someone who would otherwise be able to do a mistake".)

As to a "real time" example — there are thousands of them. Isn''t the use of sealed class reasonable without any examples? In my view, not getting it is nearly the same as not understanding of the notion of "class".

More correct question would be the opposite: why would you think that this or that class needs subclassing? For example, System.AppDomain, http://msdn.microsoft.com/en-us/library/system.appdomain.aspx[^]. Subclassing is "programming by extension". So, tell us please how would you extend Application Domain and why? Same thing about many other classes which comes with .NET. Every developer creates many classes like that, too. How about you? :-)

—SA


这篇关于密封课程的实时方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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