其他分享
首页 > 其他分享> > scala特质练习

scala特质练习

作者:互联网

object 练习 {

  abstract class Coder {
    var name: String = _
    var age: Int = _

    def eat(): Unit

    // 技能
    def skill(): Unit

  }

  class JavaCoder extends Coder {
    override def eat(): Unit = println("Java程序员吃大白菜,和大米粥")

    override def skill(): Unit = println("精通Java")
  }

  class PythonCode extends Coder {
    override def eat(): Unit = println("Python程序员吃小白菜,喝小米粥")

    override def skill(): Unit = println("精通Python")
  }

  trait BigData {
    def learningBigData(): Unit = {
      println("来到黑马之后")
      println("掌握了:Hadoop、Zookeeper、HBase、Hive、Spark、Flink等")
      println("项目经验:企业级360°全方位用户画像、千亿级数仓、黑马推荐系统、电信信号强度诊断")
    }
  }

  class PartJavaCode extends JavaCoder with BigData {
    override def eat(): Unit = {
      super.eat()
      println("精通Java + BigData的程序员,吃牛肉,和牛肉汤")
    }

    override def skill(): Unit = {
      super.skill()
      learningBigData()
    }
  }

  class PartPythonCoder extends PythonCode with BigData {
    override def eat(): Unit = {
      super.eat()
      println("精通Python + BigData的程序员,吃羊肉,喝羊肉汤")
    }

    override def skill(): Unit = {
      super.skill()
      learningBigData()
    }
  }

  def main(args: Array[String]): Unit = {
    println("普通Java程序员")
    val jc = new JavaCoder
    jc.name = "小歪歪"
    jc.age = 20
    println(jc.name, jc.age)
    jc.eat()
    jc.skill()

    println("-" * 20)

    println("精通Java + BigData的程序员")
    val pjc = new PartJavaCode
    pjc.name = "黑马一哥"
    pjc.age = 30
    println(pjc.name, pjc.age)
    pjc.eat()
    pjc.skill()
  }

}

标签:特质,scala,练习,override,def,println,skill,eat,Unit
来源: https://blog.csdn.net/m0_58535137/article/details/123027022