|
Classes
Classes are used to define an object type to be used elsewhere. Classes have
propeties just like objects, but they usually have to be declared in the class before
you use it. Any functions within a class are usually known as a method.
Classes are ideal for a variety of uses, but the most important thing aspect of
them is their reusability. It is often worth spending a reasonable amount
of time considering which classes you will need, and how they will inherit from
and interact with eachother.
Examples:
class Table
{
/* You declare variables that are used in the class, some languages would require
data type here and optionally if the property is public (can be read from or written
to from anywhere) or private (can be read from anywhere but only changed within
the object itself */
private var TableHeight;
private var TableWidth;
/* The first method of a class is known as the constructor. It is usually
used to pass in properties that would vary through different instanciations of the
class. */
public function Table(table_height, table_width)
{
TableHeight = table_height;
TableWidth = table_width;
Contents = new Array();
}
}
// Now, a class that will inherit from table
class DinnerTable extends Table
{
private
var NumSettings;
private
var Chairs;
public function DinnerTable(table_height, table_width)
{
NumSettings = 0;
NumChairs = 0;
}
} |
|