类的定义
class People {
val age:Int = 10
var name:String = _
var des:String = ""
}
修饰符
private [this] val age = 10
构造方法
// 主构造器
class Persion(val name:String, val age: Int){
def this(name: String, age: Int, gender: String) {
this(name, age) //附属构造器的第一行代码必须要调用主构造器或者其它构造器
}
}
重写
override def toString: String = "tdo to println"
伴生对象和伴生类
class ApplyTest { //对象
def apply() = {
1
}
}
Object ApplyTest { //函数
def apply() = {
new ApplyTest()
}
}
val b = ApplyTest() // ==> ApyylyTest的class实例
b.apply // ===> 1
case class
// 通常用在模式匹配
case class Dog(name: String)
println(Dog("wanc").name)
接口
- 定义接口
Tarit xxx
class xxx extends xx with xx with xx
网友评论