标签:char private data ESS containe nta mon class print
class Person{
private var id = 0
var name= "tom"
def setId(id:Int) = this.id = id
def getId() = this.id
}
var p = new Person()
p.name ="Jack"
p.name
res0: String = Jack
p.getId()
p.setId("sandy")
p.name
res1: Int = 0
res2: Unit = ()
res3: Int = 4
var mess="hello"
for(c <- mess)println(c)
mess.apply(3)
mess(3) //apply 的特殊之处在于可以简化到不写 不用。来调用,默认调用
res1: Char = l
res2: Char = l
class Person2{
def apply(name:String) = println("hello"+name)
}
var pp = new Person2()
pp.apply("tom")
pp: Person2 = Person2@4ee2a2ac
hellotom
res0: Unit = ()
class Person2{
var id =0
var name="hello"
def this(name:String){ //必须使用this,因为Scala 不能用类名来作为构造方法名
this() //必须写这一句,因为这一句是默认构造,我们不能定义了构造以后,不管午餐构造
this.name = name
}
}
var pp = new Person2("Jack")
pp.name
pp: Person2 = Person2@1f546f11
res0: String = Jack
class Person(var id:Int=0,var name:String){ } var p = new Person(1,"Jack"); p.id
需要说明是是 class Person(var id:Int=0,var name:String)
标签:char private data ESS containe nta mon class print
原文地址:https://www.cnblogs.com/feixiaobai/p/0a861d3420eac5a99acce62c2a278e93.html