class |
In many cases it is desireable to combine objects into compound
objects that behave as one object. This is possible by using the class
statement. Example:
// define a class with a Cylinder and a Box
class AB
{
var a = Box(10,10,10);
var b = Cylinder({2,2,10}, {0,7,0});
};
// create an instance of class AB
var ab = AB();
// rotate it. Box and cylinder will be rotated
ab.rotate(0,45,0);
An object of class AB will be treated as one object, as demonstrated by the
rotation. |
method |
A class may have defined methods and also the construction of an
instance can be parameterized. An example of a method is given below:
// define a class with a Cylinder and a Box
// z displacement should be given during construction
class AB(z)
{
var a = Box(10,10,z);
var b = Cylinder({2,2,z+10}, {0,7,0});
function foo(x, y)
{
a.move(x, 0, 0);
b.move(0, y, 0);
self.color = RGB(x,y,255);
};
};
// create an instance of class AB, z-displacement = 4
var ab = AB(4);
// call method foo
ab.foo(10,20);
The function foo is defined in the class scope of AB, and has two arguments x
and y. Although an instance of class AB is treated as one object, an individual
member of this object can still behave independently when it is modified
directly. This is the case when the foo function is called. |