Classroom Object Oriented Language
Designed to:
But many things are left out
class Point {
x : Int ← 0;
y : Int ← 0;
};
Cool programs are sets of class definitions:
A class is a collection of attributes and methods
Instances of a class are objects
class Point {
x : Int ← 0;
y : Int; (* use default value *)
};
The expression new Point creates a new object of class Point
An object can be thought of as a record with a slot for each attribute:

A class can also define methods for manipulating the attributes:
class Point {
x : Int ← 0;
y : Int ← 0;
movePoint(newx : Int, newy : Int): Point {
{
x ← newx;
y ← newy;
self;
} -- close block expression
}; -- close method
}; -- close class
Methods can refer to the current object using self
Information Hiding in Cool
class Point {
...
x () : Int { x };
setx (newx : Int) : Int { x ← newx };
};
Each object knows how to access the code of a method. As if the object contains a slot pointing to the code

In reality implementations save space by sharing these pointers among instances of the same class

We can extend points to colored points using subclassing(class hierarchy)
class ColorPoint inherits Point {
color : Int ← 0;
movePoint(newx : Int, newy : Int): Point {{
color ← 0;
x ← newx;
y ← newy;
self;
}};
};

Every class is a type
Base classes:
All variables must be declared(compiler infers types for expressions)
x : A;
x ← new B;
Is well typed if A is an ancestor of B in the class hierarchy(Anywhere an A is expected a B can be used)
Type safety:A well-typed program cannot result in runtime type errors

方法调用的一个例子(调用一个单参数方法m(x))

every expression has a type and a value: