为什么要使用'密封'? [英] why use the 'sealed' ?

查看:89
本文介绍了为什么要使用'密封'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何更好的理由?


-

FireCrow Studio

Kylin Garden

电子邮件:ga ******* @ gmail.com

ICQ:156134382

any better reason ?

--
FireCrow Studio
Kylin Garden
EMail:ga*******@gmail.com
ICQ:156134382

推荐答案

Kylin< ga * ******@gmail.com>写道:
Kylin <ga*******@gmail.com> wrote:
任何更好的理由?




比什么更好的理由?


使用密封当你想要覆盖一个属性/方法但是要确保它不能被进一步覆盖时,或者当你想确定一个类可以确定

时不是来自。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



Any better reason than what?

Use sealed when either you want to override a property/method but make
sure that it can''t be further overridden, or when you want to make sure
that a class can''t be derived from.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


假设你有这样的东西;


公共抽象类anAbstractClass

{

public abstract void someMethod();

public abstract void anotherMethod();

}


公共类FirstDerivativeClass:anAbstractClass

{

public sealed override void someMethod()

{

}

public override void anotherMethod()

{

}

}


公共类SecondDerivativeClass:FirstDerivativeClass

{

public override void anotherMethod()

{

}

}

公共密封类ThirdDerivativeClass:SecondDerivativeClass

{

public override void anotherMethod()

{

}

}

现在你有了一个抽象类和两个抽象方法。在

FirstDerivativeClass中,你实现了someMethod()并且不希望任何人

来覆盖它。您将其定义为密封方法。在SecondDerivativeClass你

不能覆盖someMethod()但可以覆盖anotherMethod。在

ThirdDerivativeClass中,你仍然可以覆盖anotherMethod。但你保证

通过将该类定义为

密封,不能有第四衍生类。很清楚吗?

-


谢谢,

Yunus Emre ALP?ZEN

BSc ,MCAD.NET

" Kylin" < GA ******* @ gmail.com>在消息中写道

新闻:Ob ************** @ TK2MSFTNGP12.phx.gbl ...
Suppose that you have something like this;

public abstract class anAbstractClass
{
public abstract void someMethod();
public abstract void anotherMethod();
}

public class FirstDerivativeClass : anAbstractClass
{
public sealed override void someMethod()
{
}
public override void anotherMethod()
{
}
}

public class SecondDerivativeClass : FirstDerivativeClass
{
public override void anotherMethod()
{
}
}
public sealed class ThirdDerivativeClass : SecondDerivativeClass
{
public override void anotherMethod()
{
}
}
Now you have an abstract class and two abstract methods. At
FirstDerivativeClass, you implement someMethod() and do not want to anybody
to override it. You define it as sealed method. At SecondDerivativeClass you
can not override someMethod() but can override anotherMethod. At
ThirdDerivativeClass you still can override anotherMethod. But you guarantee
that there can not be a fourth derivative class by defining that class as a
sealed. Is it clear?
--

Thanks,
Yunus Emre ALP?ZEN
BSc, MCAD.NET

"Kylin" <ga*******@gmail.com> wrote in message
news:Ob**************@TK2MSFTNGP12.phx.gbl...
还有什么更好的理由?

-
FireCrow Studio
Kylin Garden
电子邮件:ga ******* @ gmail.com
ICQ:156134382
any better reason ?

--
FireCrow Studio
Kylin Garden
EMail:ga*******@gmail.com
ICQ:156134382



作为一般规则,我避免密封我的课程。似乎总是

我想在以后添加功能。即使我确定我喜欢方法和类可以非虚拟化的想法,我也不是。而且我真的不喜欢这样的事实:静态成员总是最终确定。


虽然我准备抱怨某些事情。 NET的东西,我是

要介绍它我说我相信.NET是一个很棒的产品

仍然是第一个版本的东西。微软的工作人员做了很棒的工作。


我希望你可以添加功能(或替换成员)

C#中的现有类就像Ruby一样。 - 比如

能够将Left / Right方法添加到应用程序的

范围的字符串类中,或者用
$替换Substring函数b $ b长度参数

超过字符串末尾时不会引发异常的事情。


我踢了几个空气当我注意到有多少成员的封闭时,有多少成员 - 例如SqlDataAdapter。如果它没有被封存,并且说填充,那么方法被标记为虚拟,你可以轻松地编写一些很棒的诊断工具。


例如,假设你想要能够查看所有原始

数据集转到应用程序。所有必要的东西都是

来继承SqlDataAdapter类,用

覆盖Fill方法,比如用你的数据引发一个事件。然后编写一个

简单的Windows应用程序,每次调用Fill时,都会生成一个带有数据网格的窗口

。让那些窗口

应用程序充当您的应用程序的远程服务器,并将

应用程序设置为远程客户端。当你完成诊断时,

只需删除你的远程配置。


-Alan


Kylin写道:
As a general rule, I avoid sealing my classes. It seems that invariably
I''ll want to add functionality to something at a later date. I''m not
even sure that I like the idea that methods and classes can be
nonvirtual. And I really don''t like the fact that static members are
always final.

While I''m about to do some complaining about certain .NET things, I''m
going to introduce it by saying that I believe .NET is a great product
for something still in its first version. The guys at Microsoft did a
great job.

I sortof wish you could add functionality to (or replace members of)
existing classes in C# like you can in languages like Ruby. -- like
being able to add the Left/Right methods to the string class for the
scope of an application, or replacing the Substring function with
something that doesn''t throw an exception when the Length parameter
exceeds past the end of the string.

I kicked the air a few times when I noticed how many members of the
ADO.NET namespaces were sealed -- members like SqlDataAdapter. If it
were not sealed, and say, the "Fill" method was marked as virtual, you
could easily write some great diagnostic tools.

For instance, say you wanted the ability to view all of the raw
datasets going to an application. All that would be necessary would be
to subclass the SqlDataAdapter class, override the Fill method with
something that would, say, raise an event with your data. Then write a
simple windows application that spawned a window with a datagrid
populated with that data every time Fill was called. Have that windows
application act as a Remoting Server for your application, and set the
application to be a Remoting Client. When you''re done diagnosing,
simply remove your remoting configuration.

-Alan

Kylin wrote:
还有什么更好的理由?

-
FireCrow Studio
Kylin Garden
电子邮件:ga ******* @ gmail.com
ICQ:156134382
any better reason ?

--
FireCrow Studio
Kylin Garden
EMail:ga*******@gmail.com
ICQ:156134382






这篇关于为什么要使用'密封'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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