Using %This with Constructors
%This when used in application
class constructor code refers to the object currently being constructed.
It does not refer to the dynamic this pointer of some object under
construction. Given these two application classes, the PeopleCode %This.Test() in
the Base constructor always refers
to the Test method in the Base class.
class Base
method Base();
method Test();
method CallTest();
property string sTestResult;
end-class;
method Base
%This.Test();
end-method;
method Test
&sTestResult = "BASE_METHOD_CALLED";
end-method;
method CallTest
%This.Test();
end-method;
import PACKAGE:Base;
class Extend extends PACKAGE:Base;
method Extend();
method Test();
end-class;
method Extend
%Super = create PACKAGE:Base();
end-method;
method Test
/+ Extends/implements PACKAGE:Base.Test +/
%This.sTestResult = "EXTENSION_METHOD_CALLED";
end-method;
Even though the Extend
class method provides its own Test method which overrides Base's Test
method, in PeopleCode create Extend() which ultimately runs Base's constructor,
the %This.Test() call in Base's constructor still references Base's Test method,
not Extend's Test method, because %This always refers to the object
under construction