我无法弄清楚这部分C#代码 [英] I couldn't figure out this portion of C# code

查看:58
本文介绍了我无法弄清楚这部分C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有这段代码:

Suppose we have this fragment of code:

abstract class Expression
{
	public abstract void Evaluate();
}

class Operation : Expression
{
	public override void Evaluate()
	{
		//stuff
	}
}





现在,当我们使用如下所示的书面类:



Now, when we use the written classes somewhere like below:

Expression e = new Operation();
	
e.Evaluate();





尽管我们有两个<$ c,但运行时究竟是如何理解的$ c>评估函数并将我们的对象 e 引用到基类 Expression ,它使用子类中实现的函数?如果这不是问题,请提供温和的指导,不像参考网站或书籍中的解释。



谢谢:)



How exactly does the runtime understand despite we have two Evaluate function and have referred our object e to the base class Expression, it uses the implemented function in the child class? If it's not a problem, please provide a gentle guidance unlike explanations in reference websites or books.

Thanks :)

推荐答案

到目前为止,你没有得到任何满意的答案。这是通过虚拟方法表通过动态 dispatch 完成的。换句话说,实现了所需的灵活性抛出间接调用:在表中找到地址,对于不同的运行时类型,地址是不同的。这里解释了这个机制:

http://en.wikipedia.org/wiki/Dynamic_dispatch [ ^ ],

http://en.wikipedia.org/wiki/Virtual_method_table [ ^ ]。



如果你没有得到它,请问一些关注问题,我可以解释一下细节。



对于一些高级和间接相关的想法,请参阅我的文章:动态方法调度程序 [ ^ ]。



它没有解释OOP机制,但它讨论了dy的问题namic发送和与此机制相似,在补充OOP或滥用它?部分。



-SA
You did not get any satisfactory answers so far. This is done via dynamic dispatch via the virtual method table. In other word, the required flexibility is achieved throw indirect call: the address is found in the table, and, for different runtime types, the address is different. The mechanism is explained here:
http://en.wikipedia.org/wiki/Dynamic_dispatch[^],
http://en.wikipedia.org/wiki/Virtual_method_table[^].

If you are not getting it, ask some follow-up question, I can explain fine detail.

For some advanced and indirectly related idea, please see my article: Dynamic Method Dispatcher[^].

It does not explain OOP mechanism, but it discusses the problems of dynamic dispatch and parallels with this mechanism, in the section "Complement OOP or Abuse it?".

—SA


抽象版本只是一个定义,因此没有可执行的实现。语言中有特定的规则决定了它的完成方式,请参阅 http://msdn.microsoft.com /en-us/library/sf985hc5.aspx [ ^ ]。
The abstract version is just a definition, so there is no implementation that can be executed. There are specific rules in the language that determine how it is done, see http://msdn.microsoft.com/en-us/library/sf985hc5.aspx[^].


这是OOP的一个基本特性叫做多态,它的工作方法甚至来自基类的方法也不是抽象的(但必须是虚拟的) 。有关详细信息,请参阅下一个链接:



MSDN -Polymorphism ^ ] < br $> b $ b

示例 ^ ]
This is one of the basic feature of OOP called polymorphism, and it works even your method from base class is not abstract (but must be virtual). See the next links for details:

MSDN-Polymorphism^]

Examples^]


这篇关于我无法弄清楚这部分C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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