What's the difference between:
class Person(name: String, age: Int) {
def say = "My name is " + name + ", age " + age
}
and
class Person(val name: String, val age: Int) {
def say = "My name is " + name + ", age " + age
}
Can I declare parameters as var
s, and change their values later? For instance,
class Person(var name: String, var age: Int) {
age = happyBirthday(5)
def happyBirthday(n: Int) {
println("happy " + n + " birthday")
n
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…