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屋!