Delphi 中的抽象类 [英] Abstract Class in Delphi
问题描述
我正在使用一个包含许多抽象类的组件套件.现在我想应用多态性,但是当我创建对象时出现错误抽象类.
I am using a component suite which has many abstract classes. Now I want to apply polymorphism but I am getting Error Abstract Class when I create my object.
我是否应该覆盖所有虚拟方法,即使我不需要它?有什么变通办法或解决方案吗?
Should I override all methods which are virtual even if I don't need it? Are there any workaround or solution?
推荐答案
为了创建类的实例,您需要覆盖所有声明为虚拟抽象的方法.即使你不使用它们.
In order to make an instance of a class, you need to override all methods that are declared as virtual abstract. Even if you don't use them.
如果你真的想要变通,你可以使用空方法.但我不建议这样做.
If you really want a work around, you can use empty methods. But I won't recommend that.
并添加有关该主题的更多信息:
And to add some more information on the subject:
如果方法是用虚抽象声明的,那么它就是抽象的:
A method is abstract if it is declared with virtual abstract:
procedure MyMethod(const AMyParameter: Integer); virtual; abstract;
琐事:您甚至可以将方法重写为抽象方法:
Trivia: You can even override a method as abstract:
procedure MyMethod(const AMyParameter: Integer); override; abstract;
您需要覆盖这些方法才能从该类实例化.
You need to override these methods in order to instantiate from that class.
并且您可以将整个类声明为抽象类:
And you can declare an entire class as abstract:
type
TMyClass = class abstract (TMyAncestor)
end;
如果您尝试实例化该类,则会收到警告.
You get a warning if you try to instantiate that class.
对应的是一个密封类:
type
TMyClass = class sealed (TMyAncestor)
end;
如果您尝试从该类继承,则会收到警告.
You get a warning if you try to inherit from that class.
您也可以密封方法,但为此您需要关键字 final.
You can also seal methods, but you need the keyword final for that.
procedure MyMethod(const AMyParameter: Integer); override; final;
这篇关于Delphi 中的抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!