简体   繁体   English

重写基类的抽象方法

[英]Overriding an abstract method of base class

I have an abstract class with some methods,including an abstract method(Execute()).This method is overridden in child class.Now, an event is raised(somewhere in application),and for this event there is a handler in base class.And,in this handler,I call Execute. 我有一个带有一些方法的抽象类,其中包括一个抽象方法(Execute())。此方法在子类中被重写。现在,引发了一个事件(在应用程序中的某个位置),并且为此事件在基类中有一个处理程序并且,在此处理程序中,我称为Execute。 Now, the method of chilobject is executed.I am bit confused,how this works under the hood? 现在,执行chilobject的方法。我有点困惑,这在后台如何工作?

这是运行时多态,也称为动态绑定

One way to think of this is in terms of message passing; 想到这一点的一种方式是消息传递。 the call of Execute() inside an instance method means that the “message” Execute is sent to the current object (ie this ). 在实例方法中调用Execute()意味着将“消息” Execute发送到当前对象 (即this )。 Since the current object is an instance of the child class, it treats the received message “ Execute() ” as is defined for all instances of that class, regardless of where the command to send that message is written. 由于当前对象是子类的一个实例,因此,不管为该类的所有实例编写的是什么,它都将为该类的所有实例定义接收的消息“ Execute() ”。

(If your question was how it's technically implemented, then I'm sorry for the simplistic answer. In very general terms the class of the object is stored with the rest of its data and this is used to look up the appropriate method to call, ie how to react to a certain message to that object.) (如果您的问题是如何从技术上实现的,那么对于简单的回答,我们深感抱歉。很笼统地说,对象的类与其余数据一起存储,用于查找合适的调用方法,即如何对发送给该对象的特定消息做出反应。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM