C#中的虚方法问题 [英] C# virtual methods question

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

问题描述

有一个事情我不明白还有:当虚拟方法被调用,基本方法被调用以及



由于我在使用公共覆盖的WinForm的OnPaint方法,在它的身上base.OnPaint(五)被调用。我不明白,我以为虚拟方法覆盖原文件。
。如果它不是通常称为,为什么它在这种情况下,被称为?谢谢


解决方案

在虚方法被调用时,
基方法被称为呢?




没有。




由于当我使用公共覆盖
OnPaint()方法,在它的身上base.OnPaint(五)
之称。我不明白,我
认为虚拟方法覆盖
原之一。




的开发者选择调用base.OnPaint(E)的也做别的事情的。换句话说,基本实现你的情况下,必须在你的手的东西是很有用的你 - 你可以调用它,然后做一些额外的具体工作。



如果函数身体的只有的在底座上的电话,那么这将是等同于根本写压倒一切的功能。


There is a thing I do not understand well: when virtual method is called, the base method is called as well?

Because when I use public override WinForm OnPaint method, in its body base.OnPaint(e) is called. I do not understand it, I thought virtual methods overrides the original one. If it is not usually called, why it is called in this case? Thank you

解决方案

when virtual method is called, the base method is called as well?

No.

Because when I use public override OnPaint(), in its body base.OnPaint(e) is called. I do not understand it, I thought virtual methods overrides the original one.

The developer chose to call base.OnPaint(e) and also do something else. In other words, the base implementation is something which you have at your hand in case it is useful to you - you can call it, and then do some additional specific work.

If the function body was only a call to the base, then it would be equivalent to not writing the overriding function at all.

这篇关于C#中的虚方法问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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