Classroom Object-Oriented Language
Main
class with a special method main
Example
class Point {
x : Int <- 0;
y : Int <- 0;
};
new Point
” creates a new object of class Point
An object can be thought of as a record with a slot for each attribute
class Point {
x : Int <- 0;
y : Int; (* use default value *)
};
self
Example
class Point {
x : Int <- 0;
y : Int; (* use default value *)
movePoint(newx : Int, newy : Int) : Point {
{ x <- newx;
y <- newy;
self;
} -- close block expression
}; -- close method
}; -- close class
Example:
class Point {
x : Int <- 0;
y : Int <- 0;
getx () : Int { x };
setx (newx : Int) : Int { x <- newx };
};
Example:
class ColorPoint extends Point {
color : Int <- 0;
movePoint(newx : Int, newy : Int) : Point {
{ color <- 0;
x <- newx; y <- newy;
self;
}
};
};
Int
: integersBool
: booleansString
: for stringsObject
: root of class hierarchyExample:
x : Point;
x <- new ColorPoint;
is well-typed if Point
is an ancestor of ColorPoint
in the class hierarchy
Rephrase: … is well typed if ColorPoint
is a subtype of Point
Type safety: a well typed program cannot result in run-time type errors
Understanding dispatch in the presence of inheritance is a subtle aspect of object-oriented programming
Example:
p: Point;
p <- new ColorPoint;
p.movePoint(1,2);
p
has static type Point
p
has dynamic type ColorPoint
p.movePoint
must invoke ColorPoint
versionif E the E else E fi
while E loop E pool
case E of x : Type => E; ... esac
x <- E
out_string(E)
, in_string()
, …Memory is allocated every time “new E
” executes