Primary constructor¶
We can declare the members of a class as its parameters.
class UsedCar(val year: Int, var miles: Int) {
def drive(distance: Int) {
miles += distance
}
}
defined class UsedCar
val mycar = new UsedCar(2000, 10000)
mycar.miles
mycar: UsedCar = ammonite.$sess.cmd14$Helper$UsedCar@fa0948
res15_1: Int = 10000
mycar.drive(2000)
mycar.miles
res16_1: Int = 12000