class Student {
private var stuName: String = "Tom"
private var stuAge: Int = 20
private [this] var stuId: Int = 0
def getStuName(): String = stuName
def setStuName(newName: String): Unit = this.stuName = newName
def getStuAge(): Int = this.stuAge
def setStuAge(newAge: Int) = this.stuAge = newAge
}
// 类名与object名相同,则为伴生对象
// 如果私有属性不想外部访问 可以加[this]
object Student {
def main(args: Array[String]): Unit = {
var student = new Student
println(student.getStuName() + "\t" + student.getStuAge())
student.setStuAge(24)
student.setStuName("Mary")
println(student.getStuName() + "\t" + student.getStuAge())
//student.
}
}
Scala class 类 与 属性
最新推荐文章于 2023-02-25 23:44:24 发布