什么是以下两者之间的区别:
class Person(name: String, age: Int) {
def say = "My name is " + name + ", age " + age
}
andclass Person(val name: String, val age: Int) {
def say = "My name is " + name + ", age " + age
}
我能将参数声明为 var
并在以后更改它们的值吗?例如,
class Person(var name: String, var age: Int) {
age = happyBirthday(5)
def happyBirthday(n: Int) {
println("happy " + n + " birthday")
n
}
}