« Previous: Lambdas and Closures · Guide Index · Next: Error Handling »
A class bundles data (fields) with behaviour (methods). Calling the class creates an instance, also called an object. Objects have shared identity: two names can refer to the very same object.
class Person {
init(name) {
self.name = name
}
greet() {
print(self.name)
}
}
p = Person("Alice")
p.greet() # Alice
initis the constructor. It runs when the class is called.selfrefers to the current instance.- Fields are created by assigning to
self.field.
Each call to the class produces a new, independent object:
a = Person("Bob")
b = Person("Charlie")
a.greet() # Bob
b.greet() # Charlie
A class with no init can still be instantiated:
class Empty {
}
e = Empty()
print("created")
Methods are func-like but belong to the instance and receive self:
class Counter {
init() {
self.x = 0
}
inc() {
self.x = self.x + 1
}
get() {
return self.x
}
}
c = Counter()
c.inc()
c.inc()
print(c.x) # 2
print(c.get()) # 2
Methods can call other methods and take arguments:
class Math {
add(x, y) {
return x + y
}
double(x) {
return x * 2
}
}
m = Math()
print(m.add(3, 4)) # 7
print(m.double(m.add(1, 2))) # 6
Write one statement per line inside a multi-statement method body. This is required by the parser; see Style and Limits.
Objects are references, not copies. Assigning an object to another name makes both names point at the same instance:
class Counter {
init() {
self.x = 0
}
inc() {
self.x = self.x + 1
}
get() {
return self.x
}
}
a = Counter()
b = a # b and a refer to the SAME object
b.inc()
print(a.get()) # 1
Fields can hold other objects, and you can chain field access:
class Inner {
init(v) {
self.v = v
}
}
class Outer {
init() {
self.inner = Inner(77)
}
}
o = Outer()
print(o.inner.v) # 77
Error Handling covers try and oops.
« Previous: Lambdas and Closures · Guide Index · Next: Error Handling »