Delphi中的抽象类

发布于 2024-11-10 12:06:35 字数 96 浏览 5 评论 0原文

我正在使用一个具有许多抽象类的组件套件。现在我想应用多态性,但在创建对象时收到错误抽象类。

即使我不需要,我是否应该重写所有虚拟方法?有什么解决方法或解决方案吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终遇你 2024-11-17 12:06:36

为了创建类的实例,您需要重写所有声明为虚拟抽象的方法。即使您不使用它们。

如果您确实想要解决方法,可以使用空方法。但我不会推荐这样做。

并添加有关该主题的更多信息:

如果使用虚拟抽象声明方法,则该方法是抽象的:

procedure MyMethod(const AMyParameter: Integer); virtual; abstract;

琐事:您甚至可以将方法重写为抽象方法:

procedure MyMethod(const AMyParameter: Integer); override; abstract;

您需要重写这些方法才能从该类实例化。

您可以将整个类声明为抽象类:

type
  TMyClass = class abstract (TMyAncestor)
  end;

如果您尝试实例化该类,您会收到警告。

对应的是密封类:

type
  TMyClass = class sealed (TMyAncestor)
  end;

如果您尝试从该类继承,您会收到警告。

您还可以密封方法,但需要关键字final。

procedure MyMethod(const AMyParameter: Integer); override; final;

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.

The counterpart is a sealed class:

type
  TMyClass = class sealed (TMyAncestor)
  end;

You get a warning if you try to inherit from that class.

You can also seal methods, but you need the keyword final for that.

procedure MyMethod(const AMyParameter: Integer); override; final;
末蓝 2024-11-17 12:06:36

Delphi 没有抽象类,只有抽象方法。如果调用抽象方法,将会引发抽象方法异常。

简而言之,您不能调用抽象方法。如果编译器检测到您使用抽象方法实例化类,则会发出警告。好的做法是要求编译器将这些警告转化为错误。

Delphi doesn't have abstract classes as such, only abstract methods. You will get an abstract method exception raised if you call an abstract method.

Put simply you must not call abstract methods. The compiler emits a warming if it detects you instantiating a class with abstract methods. Good practise is to ask the compiler to turn these warnings into errors.

梦在深巷 2024-11-17 12:06:36

那么它仍然是抽象的

  1. 如果您的后代类被声明为抽象,或者
  2. 它至少有一个声明为抽象的方法,或者
  3. 它没有覆盖和实现其祖先的所有抽象方法,

Your descendant class is still abstract if

  1. it's declared abstract, or
  2. it has at least one method declared abstract, or
  3. it doesn't override and implement all abstract methods from its ancestors
奢华的一滴泪 2024-11-17 12:06:36

如果您重写抽象构造函数,它将产生错误,因为它会自动将继承放入新构造函数中,如果您使用代码自动完成,新构造函数当然会调用抽象构造函数。

例如

type
  TMyclass = class (TObject)
  public
    constructor Create(AOwner : TComponent); dynamic; abstract;
  end;

  TMyclass2 = class(TMyclass)
  public
    Constructor Create(AOwner : TComponent); override;
  end;

implementation

constructor TMyclass2.Create(AOwner: TComponent);
begin
  inherited;

end;

It will produce an error if you override the abstract constructor as it automatically puts inherited into the new constructor which of course is calling the abstract constructor if you use the code auto complete.

e.g.

type
  TMyclass = class (TObject)
  public
    constructor Create(AOwner : TComponent); dynamic; abstract;
  end;

  TMyclass2 = class(TMyclass)
  public
    Constructor Create(AOwner : TComponent); override;
  end;

implementation

constructor TMyclass2.Create(AOwner: TComponent);
begin
  inherited;

end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文