Smart pascal source code
{
Demo: Object Orientated Programming (OOP)
}
type
AClass = class
function Q: string;
end;
type
BClass = class(AClass)
function Q: string;
end;
type
CClass = class
function Q: string;
end;
function AClass.Q: string;
begin
Result := 'AClass.Q: Static method';
end;
function BClass.Q: string;
begin
Result := 'BClass.Q: Static method';
end;
function CClass.Q: string;
begin
Result := 'CClass.Q: Static method';
end;
var objA, objB: AClass;
objA := AClass.Create;
objB := BClass.Create;
if objA is AClass then
WriteLn('objA is AClass');
if objA is BClass then
WriteLn('objA is BClass');
if objA is CClass then
WriteLn('objA is CClass');
if objB is AClass then
WriteLn('objB is AClass');
if objB is BClass then
WriteLn('objB is BClass');
if objB is CClass then
WriteLn('objB is CClass');
WriteLn((objA as AClass).Q);
try
WriteLn((objA as BClass).Q);
except
WriteLn('objA isn''t BClass');
end;
try
WriteLn((objA as CClass).Q);
except
WriteLn('objA isn''t CClass');
end;
WriteLn((objB as AClass).Q);
WriteLn((objB as BClass).Q);
try
WriteLn((objB as CClass).Q);
except
WriteLn('objB isn''t CClass');
end;
{<<< RESULT - CONSOLE LOG >>>
-----------------------------
Errors >>>>
Warning: Incompatible types: "AClass" and "CClass" [line: 42, column: 9]
Warning: Incompatible types: "AClass" and "CClass" [line: 46, column: 9]
Result >>>>
objA is AClass
objB is AClass
objB is BClass
AClass.Q: Static method
objA isn't BClass
objA isn't CClass
AClass.Q: Static method
BClass.Q: Static method
objB isn't CClass
-----------------------------
{<<<<<<<<< THE END >>>>>>>>>}
var CClass = {
$ClassName:"CClass",$Parent:TObject
,$Init:function ($) {
TObject.$Init($);
}
,Q:function(Self) {
return "CClass.Q: Static method";
}
,Destroy:TObject.Destroy
};
var AClass = {
$ClassName:"AClass",$Parent:TObject
,$Init:function ($) {
TObject.$Init($);
}
,Q$1:function(Self) {
return "AClass.Q: Static method";
}
,Destroy:TObject.Destroy
};
var BClass = {
$ClassName:"BClass",$Parent:AClass
,$Init:function ($) {
AClass.$Init($);
}
,Q$2:function(Self) {
return "BClass.Q: Static method";
}
,Destroy:TObject.Destroy
};
var objA = null;
var objB = null;
/* <<< main JS >>> */
objA = TObject.Create($New(AClass));
objB = TObject.Create($New(BClass));
if ($Is(objA,AClass)) {
WriteLn("objA is AClass");
}
if ($Is(objA,BClass)) {
WriteLn("objA is BClass");
}
if ($Is(objA,CClass)) {
WriteLn("objA is CClass");
}
if ($Is(objB,AClass)) {
WriteLn("objB is AClass");
}
if ($Is(objB,BClass)) {
WriteLn("objB is BClass");
}
if ($Is(objB,CClass)) {
WriteLn("objB is CClass");
}
WriteLn(AClass.Q$1(objA));
try {
WriteLn(BClass.Q$2($As(objA,BClass)));
} catch ($e) {
WriteLn("objA isn't BClass");
}
try {
WriteLn(CClass.Q($As(objA,CClass)));
} catch ($e) {
WriteLn("objA isn't CClass");
}
WriteLn(AClass.Q$1(objB));
WriteLn(BClass.Q$2($As(objB,BClass)));
try {
WriteLn(CClass.Q($As(objB,CClass)));
} catch ($e) {
WriteLn("objB isn't CClass");
}