美文网首页
scala对象

scala对象

作者: 松松土_0b13 | 来源:发表于2019-10-13 18:17 被阅读0次

类的定义

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

相关文章

网友评论

      本文标题:scala对象

      本文链接:https://www.haomeiwen.com/subject/ibkfmctx.html