class Person (name:String, val age:Int, private val sex:Boolean) {
def getName = {
name
}
def getSex = {
sex match {
case true => "Boy"
case false => "girl"
}
}
}
scala> val xue = new Person("xuelanghu", 21, true)
xue: Person = Person@370a646b
scala> xue.name
<console>:19: error: value name is not a member of Person
xue.name
^
scala> xue.age
res17: Int = 21
scala> xue.sex
<console>:19: error: value sex in class Person cannot be accessed in Person
xue.sex
^
scala> xue.getName
res19: String = xuelanghu
scala> xue.getSex
res20: String = Boy
// attention that two error are different, when you use a `val` or `var`
// means this arg is a filed of this class. otherwise, it just a parameter
// which can still be referenced by other members of the class, because it
// is in the scope of the entire body of the class.
Args in class constructor
最新推荐文章于 2024-08-03 18:12:37 发布