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

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

问题描述

在 Delphi 中使用 reintroduce 关键字的动机是什么?

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

如果你的子类包含一个与父类中的虚函数同名的函数,并且它没有用 override 修饰符声明,那么这是一个编译错误.在这种情况下添加 reintroduce 修饰符修复了错误,但我一直没有理解编译错误的原因.

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. 添加关键字 reintroduce 只是告诉编译器您知道您正在隐藏该方法,并且它会抑制警告.您仍然可以在该继承方法的实现中使用 inherited 关键字来调用祖先方法.
  2. 如果祖先的方法是虚拟动态,那么您可以使用覆盖.它有一个附加的行为,如果这个后代对象是通过祖先类型的表达式访问的,那么对该方法的调用仍然是对后代方法的调用(然后可以选择通过 inherited).
  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).

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

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.)

不管 Malach 怎么说,您仍然可以在重新引入的方法中调用继承,即使父级既不是虚拟也不是<强>动态.

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.

进一步说明:

重新引入是一种向编译器传达您没有出错的意图的方式.我们使用 override 关键字覆盖祖先中的方法,但它要求祖先方法是虚拟动态,并且您希望当对象作为祖先类访问时要更改的行为.现在输入重新引入.它让你告诉编译器你没有意外地创建一个与虚拟或动态祖先方法同名的方法(如果编译器没有警告你,这会很烦人).

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天全站免登陆