Difference between Override and Reintroduce in Delphi

来源:互联网 发布:上搜狐知天下 编辑:程序博客网 时间:2024/06/05 00:45
原文地址:http://techblogsearch.com/a/difference-between-override-and-reintroduce-in-delphi.html

Difference between Override and Reintroduce in Delphi

When you decide to declare a method as virtual, you are giving permission to derived classes to extend and override the method with their own implementation.
当你决定去声明一个虚方法时,就表示你给了继承类用他们自己的实现去扩展获取重写这个方法的许可。

Use the reintroduce keyword to introduce a new implementation of a parent method (this hides the parent method). You can hide a method without using reintroduce but you will get a compiler warning. Using reintroduce will suppress the warning.

You tell the compiler that you know that you hide the ancestor function and replace it with this new function and do so deliberately.

Difference between Override and Reintroduce

1. The reintroduce and override modifiers have different meanings. The reintroduce modifier creates a new member with the same name, signature, and visibility and hides the original member. The override modifier extends the implementation for an inherited member and allows you to implement inheritance-based polymorphism.

2. Override is used in conjuction with Virtual/Dynamic methods of parent class but for Reintroduce, parent method should not be necassarily Virtual/Dynamic. 

The only reason to override an ancestor method is that you can call inherited from within the descendent method. If you don't intend to call inherited from within a descendent method, the descendent method should be declared with Reintroduce rather than Override.

Avoid Reintroduce

Sometimes there are clear reasons to introduce a new method with the same name, signature, and visibility of a parent method. In those clear cases, introducing a new member is a powerful feature. However, if you do not have a clear reason, then avoid using Reintroduce.
0 0