特质

18 阅读1分钟

**

  • 特质: trait
  • 它用来实现多继承
package Level02


object class09 {

  trait Tall {
    var height: Int = 180
  }

  trait Eye {
    var beautifulEye: String = "大眼睛"
  }

  // class 就有两个父类
  class Girl extends Tall with Eye {}

  def main(args: Array[String]): Unit = {
    val girl1 = new Girl()

println(girl1.beautifulEye)
println(girl1.height)

  }
}

image.png