delphi - Create generic class or interface with self typed parameters in children classes -
i'd create class or interface subclass, using current class instances methods parameters...
here example explain problem:
type iarithmeticobject = interface(iinterface) procedure assign(ao : iarithmeticobject); procedure add(ao : iarithmeticobject); procedure remove(ao : iarithmeticobject); procedure multiply(ao : iarithmeticobject); procedure divide(ao : iarithmeticobject); end;
the interface iarithmeticobject
whould starting point, referencing basic arithmetic operations , child classes declared as
type tinteger = class(tinterfacedobject, iarithmeticobject) procedure assign(ao : tinteger); procedure add(ao : tinteger); procedure remove(ao : tinteger); procedure multiply(ao : tinteger); procedure divide(ao : tinteger); end;
with parameter type ao
being tinteger
, not iarithmeticobject
.
another idea use self referencing generic type like:
amathobject = class; amathobject<t : amathobject, constructor> = class procedure assign(ao : t);virtual;abstract; procedure add(ao : t);virtual;abstract; procedure remove(ao : t);virtual;abstract; procedure multiply(ao : t);overload;virtual;abstract; procedure divide(ao : t);virtual;abstract; end;
but not figure out right syntax...
does have ideas possibility (or impossibility)?
if undestand correctly, may want derive class generic interface.
type iarithmeticobject<t> = interface procedure assign(ao: iarithmeticobject<t>); procedure add(ao: iarithmeticobject<t>); procedure remove(ao: iarithmeticobject<t>); procedure multiply(ao: iarithmeticobject<t>); procedure divide(ao: iarithmeticobject<t>); end; tinteger = class (tinterfacedobject, iarithmeticobject<tinteger>) procedure assign(ao: iarithmeticobject<tinteger>); procedure add(ao: iarithmeticobject<tinteger>); procedure remove(ao: iarithmeticobject<tinteger>); procedure multiply(ao: iarithmeticobject<tinteger>); procedure divide(ao: iarithmeticobject<tinteger>); end;
answer edited according stefan glienke's comment: methods of class accept parameters declared either objects or interfaces.
var ao: iarithmeticobject<tinteger>; begin ao := tinteger.create; ao.multiply(ao); end.
Comments
Post a Comment