Inheritance and Specialization of Methods and Functions - Gnorion/BizVR GitHub Wiki
Inheritance and Specialization of Methods and Functions
Method Inheritance with shapes Here's an example of inheritance and specialization using methods
define SHAPE: // abstract - no attributes
method:area() // one method with no implementation - all shapes have area
define SQUARE < SHAPE // square is a subclass of shape
side number // with attribute side
method:area() = side * side // and area side * side
define CIRCLE < SHAPE // circle is a subclass of shape
radius number // it has one attribute radius
method:area() = pi * radius**2 // it specializes the area method
let s1 = SQUARE.new(side=5) // create a square
let c1 = CIRCLE.new(radius=3) // create a circle
let shapes = {s1,c1} // a collection of two different shape subclasses
for s in shapes // for every shape
if s.area() > 10 then message end // find their areas
end
How would this be accomplished with functions?
Does this approach make sense?
Functions not defined as part of a class, but arguments may be instances of the classes)
function:area(SHAPE) // this doesn't seem necessary
function:area(SQUARE) = square.side * square.side // defines area for a square
function:area(CIRCLE) = pi * circle.radius**2 // defines area for a cicle
// basically area is an overloaded function in this example
define SHAPE: // abstract - no attributes
define SQUARE < SHAPE // square is a subclass of shape
side number // with attribute side
define CIRCLE < SHAPE // circle is a subclass of shape
radius number // it has one attribute radius
let s1 = SQUARE.new(side=5) // create a square
let c1 = CIRCLE.new(radius=3) // create a circle
let shapes = {s1,c1} // a collection of two different shape subclasses
for s in shapes // for every shape
if area(s) > 10 then message end // find the area
end