事件与方法覆盖 [英] event vs method override

查看:68
本文介绍了事件与方法覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



与方法覆盖相比,有人能指出一些关于

事件使用的最佳实践文档。

防爆。在Windows窗体中应该什么时候我有一个Paint的事件处理程序,当

应该覆盖OnPaint?


我必须在子类中实现附加功能,并且我在两个

头脑中..让父母开火一个事件,或者让方法虚拟,并且孩子将覆盖
覆盖。我希望一些阅读描述各种

scenerios将增加我的理解,并最终帮助我做出

决定。


thanx提前

-bh

Hi,
Can someone point me to some good best practices kind of documentation on
use of events compared to method overriding.
Ex. In Windows Forms when should i have an event handler for Paint, and when
should i override OnPaint?

I have to implement added functionality in a child class, and am in two
minds .. to have the parent fire an event, or make the method virtual and
override in the child. Am hoping some reading with description of various
scenerios will increase my understanding and eventually help me make the
decision.

thanx in advance
-bh

推荐答案

我不能想到一份文件,抱歉。

但是我可以给你我的看法。


选择使用覆盖强制你的用户继承类,所以

只选择覆盖为一个选项,当你期望用户通过修改什么来定制你的班级的行为(这通常不是一个非常好的b / b
)通常会是你的班级

的实施细节。


当你想要通知某些事情时选择活动,这样他们就可能有可能

离开并做其他事情,考虑多个对象的情况

可以订阅该事件,这是覆盖不可能的事情。 />

-

John Wood

博客: http://spaces.msn.com/members/johnwood/

" bhavin" < bh@somecom.com>在消息中写道

新闻:eS ************** @ TK2MSFTNGP10.phx.gbl ...
I can''t think of a document, sorry.
But I can give you my opinion.

Choosing to use an override forces your user to inherit from the class, so
only choose override as an option when you''d be expecting the user to
customize the behavior of your class (which itself isn''t generally a very
common thing) by modifying what would normally be implementation details of
your class.

Choose events when you want to notify of something so they can potentially
go off and do something else, consider situations where more than one object
may subscribe to the event, something which isn''t possible with overrides.

--
John Wood
Blog: http://spaces.msn.com/members/johnwood/
"bhavin" <bh@somecom.com> wrote in message
news:eS**************@TK2MSFTNGP10.phx.gbl...
与方法重写相比,有人能指出一些关于事件使用的文档的最佳实践文档。
Ex。在Windows窗体中应该什么时候我有Paint的事件处理程序,
何时应该覆盖OnPaint?

我必须在子类中实现添加的功能,并且我在两个
介意..让父母开火一个事件,或者让方法虚拟并在孩子身上覆盖。我希望一些阅读与各种
scenerios的描述将增加我的理解,并最终帮助我做出
决定。

提前
-bh
Hi,
Can someone point me to some good best practices kind of documentation on
use of events compared to method overriding.
Ex. In Windows Forms when should i have an event handler for Paint, and when should i override OnPaint?

I have to implement added functionality in a child class, and am in two
minds .. to have the parent fire an event, or make the method virtual and
override in the child. Am hoping some reading with description of various
scenerios will increase my understanding and eventually help me make the
decision.

thanx in advance
-bh



覆盖和添加事件处理程序之间有两个重要的区别


1)要覆盖你需要是一个派生类(显然),而任何人都可以向控件添加一个事件处理程序。


2)这些虚方法的基类版本触发事件处理程序。因此,如果您需要在特定位置对代码进行分层,相对于事件处理程序触发时(之前或之后)覆盖是唯一可靠的方法。 Paint事件就是一个很好的例子。通常,人们添加一个绘制事件处理程序来装饰。你的标准外观控制。在事件处理程序运行之前,您需要确保控件描绘它的外观,否则您将在装饰的顶部绘制。所以下面是自定义控件的标准模式


protected override void OnPaint(PaintEventArgs e)

{

//在这里做你的自定义绘画


base.OnPaint(e); //这会激活事件处理程序

}


问候


Richard Blewett - DevelopMentor
< a rel =nofollowhref =http://www.dotnetconsult.co.uk/weblogtarget =_ blank> http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk



与方法覆盖相比,有人能指出一些关于

事件使用的良好最佳实践文档。

Ex。在Windows窗体中应该什么时候我有一个Paint的事件处理程序,当

应该覆盖OnPaint?


我必须在子类中实现附加功能,并且我在两个

头脑中..让父母开火一个事件,或者让方法虚拟,并且孩子将覆盖
覆盖。我希望一些阅读描述各种

scenerios将增加我的理解,并最终帮助我做出

决定。


thanx提前

-bh

There are two crucial differences between overriding and adding an event handler

1) To override you need to be a derived class (obviously) whereas anyone can add an event handler to a control.

2) The base class version of these virtual methods fires the event handlers. So if you need to layer in code in a specific place relative to when the event handlers fire (before or after) overriding is the only sure-fire way of doing this. The Paint event is a great example of this. Normally, people add a paint event handler to "decorate" your control from its standard appearance. You need to make sure the control paints it''s appearance before the event handlers run otherwise you will draw over the top of the decorations. So the folllowing is the standard pattern for a custom control

protected override void OnPaint(PaintEventArgs e)
{
// Do your custom painting here

base.OnPaint(e); // this fires thte event handlers
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi,
Can someone point me to some good best practices kind of documentation on
use of events compared to method overriding.
Ex. In Windows Forms when should i have an event handler for Paint, and when
should i override OnPaint?

I have to implement added functionality in a child class, and am in two
minds .. to have the parent fire an event, or make the method virtual and
override in the child. Am hoping some reading with description of various
scenerios will increase my understanding and eventually help me make the
decision.

thanx in advance
-bh


http://www.bobpowell.net/overrideorhandle.htm


Peace,

--Carl

http://www.bobpowell.net/overrideorhandle.htm

Peace,
--Carl


这篇关于事件与方法覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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