Designing Classes
A class is a template for objects that share the same structure and behavior. In Clean Language, you declare properties at the top of the class, write a constructor that sets them, and add methods that operate on them. The compiler enforces everything.
Part of: How Clean works
A class with properties, a constructor, and multiple methods:
start:
Rectangle r = Rectangle(5.0, 3.0)
print(r.describe())
print("Perimeter: " + r.perimeter().toString())
class Rectangle
number width
number height
constructor(number w, number h)
width = w
height = h
functions:
public:
number area()
return width * height
number perimeter()
return 2.0 * (width + height)
string describe()
return "Rectangle " + width.toString() + "x" + height.toString() + ", area: " + area().toString()Rectangle 5x3, area: 15
Perimeter: 16Properties (width, height) are declared at the top of the class. The constructor assigns them. Methods access properties by name — no self. or this. needed. Methods can call other methods directly: describe() calls area() to avoid duplicating logic.
Each instance is independent — they share the template but have their own data:
start:
Rectangle small = Rectangle(2.0, 3.0)
Rectangle large = Rectangle(10.0, 8.0)
print("Small: " + small.describe())
print("Large: " + large.describe())
boolean fits = small.area() < large.area()
print("Small fits inside large: " + fits.toString())
number total = small.area() + large.area()
print("Combined area: " + total.toString())Small: Rectangle 2x3, area: 6
Large: Rectangle 10x8, area: 80
Small fits inside large: true
Combined area: 86small and large are independent instances. Calling .area() on small uses 2.0x3.0; on large it uses 10.0x8.0. You can use instance method results in expressions, comparisons, and other calculations just like any other value.
- Properties are declared at the top of the class body with type and name
- constructor() assigns initial values — called when you create an instance
- Methods access properties by name directly — no self or this keyword
- Each instance is independent — its properties are separate from every other instance