Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi : Constructing Abstract Class like Java

Just curious, can i construct an abstract class in Delphi like Java Style?

Java Declaration:

public abstract class Foo {
  public abstract void test();
}

Way to use :

public void testCsontruct() {
  Foo clsfoo = new Foo(){
    public void test(){
      //....do something here

    }
  };
}

Delphi declaration :

Tfoo = class
public
  procedure test; virtual; abstract;
end;

Way to use :

procedure testUse;
var
  foo:Tfoo;
begin
  foo:=Tfoo.Create; //can we implement the test method here ? how?
end;
like image 924
Theo Avatar asked Sep 16 '26 05:09

Theo


1 Answers

That Java functionality is the anonymous class. They are useful in a number of scenarios. As well as that which you illustrate, they are commonly used to capture variables in the enclosing scope.

There is nothing quite like that syntax in Delphi. Anonymous methods come close, but there's no syntax to allow you to replace a virtual method of an instantiated object with an anonymous methods.

You need to sub-class to provide a concrete implementation of the virtual method. You can sub-class at compile time in the usual way, or at run time using virtual method interceptors or some similar VMT trickery.

like image 170
David Heffernan Avatar answered Sep 18 '26 18:09

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!