在Delphi中重新引入函数 [英] Reintroducing functions in Delphi

查看:136
本文介绍了在Delphi中重新引入函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果您有一个小孩类,那么在Delphi中使用重新引入关键字的动机是什么?包含与父类中的虚函数具有相同名称的函数,并且不使用override修饰符声明它,那么它是一个编译错误。在这种情况下添加重新引入修饰符会修复错误,但是我从来没有抓住编译错误的原因。

解决方案

在后代类中声明一个与祖先类中的方法具有相同名称的方法,那么您将隐藏该祖先方法 - 这意味着如果您有一个该后代类的实例(被引用为该类),则不会得到祖先的行为。当祖先的方法是虚拟的或动态的,编译器会给你一个警告。



现在你有两个选择之一来抑制这个警告消息:


  1. 添加关键字 reintroduce 只是告诉编译器你知道你隐藏该方法,它会抑制警告。您仍然可以在执行该后代方法的继承的关键字中调用祖先方法。

  2. 如果祖先的方法是虚拟动态,则可以使用覆盖。它还增加了行为,如果通过祖先类型的表达式访问此后代对象,那么对该方法的调用仍将是后代方法(然后可以通过继承调用祖先) )

所以覆盖重新引入之间的区别是多态。使用重新引入,如果将后代对象转换为父类型,则调用该方法可以获取祖先方法,但是如果您访问了后代类型,那么您将获得后代的行为。通过覆盖,你总是得到后代。如果祖先方法既不是虚拟也不是动态,则重新引入不适用,因为该行为是隐式的。 (实际上你可以使用助手,但我们现在不会去那里)。



尽管Malach说,你仍然可以调用继承 ,即使父母既不是虚拟也不是动态



基本上重新引入就像覆盖,但它适用于非动态和非虚拟方法,而如果通过祖先类型的表达式访问对象实例,则不会替换行为。



进一步说明:



重新引入是向编译器传达意图的方式,您没有发出错误。我们使用覆盖关键字覆盖祖先中的方法,但它要求祖先方法为虚拟动态,而您希望当对象作为祖先类访问时,行为更改。现在输入重新引入。它可以让您告诉编译器您没有意外创建与虚拟或动态祖先方法具有相同名称的方法(如果编译器没有向您发出警告,那将会令人讨厌)。


What was the motivation for having the reintroduce keyword in Delphi?

If you have a child class that contains a function with the same name as a virtual function in the parent class and it is not declared with the override modifier then it is a compile error. Adding the reintroduce modifier in such situations fixes the error, but I have never grasped the reasoning for the compile error.

解决方案

If you declare a method in a descendant class that has the same name as a method in an ancestor class then you are hiding that ancestor method — meaning if you have an instance of that descendant class (that is referenced as that class) then you will not get the behavior of the ancestor. When the ancestor's method is virtual or dynamic, the compiler will give you a warning.

Now you have one of two choices to suppress that warning message:

  1. Adding the keyword reintroduce just tells the compiler you know you are hiding that method and it suppresses the warning. You can still use the inherited keyword within your implementation of that descended method to call the ancestor method.
  2. If the ancestor's method was virtual or dynamic then you can use override. It has the added behavior that if this descendant object is accessed through an expression of the ancestor type, then the call to that method will still be to the descendant method (which then may optionally call the ancestor through inherited).

So difference between override and reintroduce is in polymorphism. With reintroduce, if you cast the descendant object as the parent type, then call that method you will get the ancestor method, but if you access it the descendant type then you will get the behavior of the descendant. With override you always get the descendant. If the ancestor method was neither virtual nor dynamic, then reintroduce does not apply because that behavior is implicit. (Actually you could use a class helper, but we won't go there now.)

In spite of what Malach said, you can still call inherited in a reintroduced method, even if the parent was neither virtual nor dynamic.

Essentially reintroduce is just like override, but it works with non-dynamic and non-virtual methods, and it does not replace the behavior if the object instance is accessed via an expression of the ancestor type.

Further Explanation:

Reintroduce is a way of communicating intent to the compiler that you did not make an error. We override a method in an ancestor with the override keyword, but it requires that the ancestor method be virtual or dynamic, and that you want the behavior to change when the object is accessed as the ancestor class. Now enter reintroduce. It lets you tell the compiler that you did not accidentally create a method with the same name as a virtual or dynamic ancestor method (which would be annoying if the compiler didn't warn you about).

这篇关于在Delphi中重新引入函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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