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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With