Inheritance in BEE is done by the child's constructor calling the parent's, so the object have all the child's variables (object attributes) and functions (method), PLUS all the parent's variables and functions.
In the following example, Vehicle has two Child classes: car and plane. We use the same constructor vehicle in the previous example and create two more:
function vehicle
{
var this%make = "{arg%m}";
var this%capacity = "{arg%c}";
var this%function:do = "dontknow";
}
function car
{
this%vehicle m="{arg%m}" c="{arg%c}";
var this%wheel = "{arg%w}";
var this%function:do = "drive";
}
function plane
{
this%vehicle m="{arg%m}" c="{arg%c}";
var this%engine = "{arg%e}";
var this%function:do = "fly";
}
function drive
{
display "{arg%who} drive {arg%this}<br>
";
}
function fly
{
display "{arg%who} fly {arg%this}<br>
";
}
function dontknow
{
display "{arg%who} don't know what to do with {arg%this}<br>
";
}
var myCar% = new car m="Toyota" c="4" w="alloy";
var myPlane% = new plane m="Boing" c="400" e="jet";
var mySpaceship% = new vehicle m="Alien Nation" c="4000";
myCar%do who="I";
myPlane%do who="You";
mySpaceship%do who="We";
Note: When an object function needs to call another function of the same object, it is recommended to call into the absolute name instead of the object function name in case the callee function is overloaded. For example:
function vehicle
{
var this%make = "{arg%m}";
var this%capacity = "{arg%c}";
var this%function:do = "dontknow";
var this%function:start = "vehicle_start";
var this%function:findkey = "vehicle_findkey";
}
function car
{
this%vehicle m="{arg%m}" c="{arg%c}";
var this%wheel = "{arg%w}";
var this%function:do = "drive";
}
function vehicle_start
{
this%findkey; // For safety, replace this by this%vehicle_findkey;
display "Start Vehicle.<br>
";
}
function vehicle_findkey
{
display "Find the key of the Vehicle.<br>
";
}
The above worked fine, but if the constructor of "car" overload the function findkey (e.g. var this%function:findkey = "car_findkey";), then vehicle_start will call into "car_findkey". If this is not your intention, please replace "this%findkey;" with "this%vehicle_findkey;".
Calling on the actual function name allows the children object to call an overloaded parent function without ambiguity.
|