非抽象类不提供抽象方法的实现

时间:2022-02-25 20:46:54

I have a base class like the following.

我有一个类如下的基类。

TMakerObject = class
...
public
    method Clone:TControlObject; virtual; abstract;
end;

I want to make the method clone abstract. So, base class doesn't need to implement or define this method. However, the subclasses can define their own clone method.

我想让方法克隆抽象。因此,基类不需要实现或定义此方法。但是,子类可以定义自己的克隆方法。

But Compiler keep giving me this error - non-abstract class does not provide implementation for abstract method.

但编译器继续给我这个错误 - 非抽象类不提供抽象方法的实现。

If so, then how is this done?

如果是这样,那怎么办?

Thanks,

谢谢,

1 个解决方案

#1


1  

(this isn't specific to Delphi) By saying the method is abstract you are saying that the base class does not define an implementation for it. So there are two options for how the language might behave:

(这不是特定于Delphi)通过说该方法是抽象的,你说基类没有为它定义一个实现。因此,语言的行为方式有两种选择:

  • You're allowed to have the base class as non-abstract, and calls to this method on an instance of the base class fail at execution time with an error along the lines of 'method not implemented'; or
  • 您被允许将基类作为非抽象类,并且在基类的实例上对此方法的调用在执行时失败,并且出现“方法未实现”的错误;要么
  • You're not allowed to have a class contain an abstract method unless it itself is also declared abstract, so that instances of it can't exist, and the above problem never happens.
  • 你不允许一个类包含一个抽象方法,除非它本身也被声明为abstract,因此它的实例不能存在,并且上面的问题永远不会发生。

Favouring compile-time problems over execution-time ones, the language designers went with the second option.

语言设计人员在第二个选项中使用了编译时问题,而不是执行时问题。

#1


1  

(this isn't specific to Delphi) By saying the method is abstract you are saying that the base class does not define an implementation for it. So there are two options for how the language might behave:

(这不是特定于Delphi)通过说该方法是抽象的,你说基类没有为它定义一个实现。因此,语言的行为方式有两种选择:

  • You're allowed to have the base class as non-abstract, and calls to this method on an instance of the base class fail at execution time with an error along the lines of 'method not implemented'; or
  • 您被允许将基类作为非抽象类,并且在基类的实例上对此方法的调用在执行时失败,并且出现“方法未实现”的错误;要么
  • You're not allowed to have a class contain an abstract method unless it itself is also declared abstract, so that instances of it can't exist, and the above problem never happens.
  • 你不允许一个类包含一个抽象方法,除非它本身也被声明为abstract,因此它的实例不能存在,并且上面的问题永远不会发生。

Favouring compile-time problems over execution-time ones, the language designers went with the second option.

语言设计人员在第二个选项中使用了编译时问题,而不是执行时问题。