scala中Trait简单使用

Posted yxj0728

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了scala中Trait简单使用相关的知识,希望对你有一定的参考价值。

trait Log {

  def log(message:String) = println("log:" + message)

}


/**
  * 为实例混入trait
  *
  */

trait Logs{
  def log(message:String){}
}

trait MyLog extends Logs {
  override def log(message: String): Unit = {
    println("log:" + message)
  }
}

class MixTraitUse(val name:String) extends Logs {

  def sayHello{
    println("hello " + name)
    log("sayHello method invoked!!")
  }

}


object MixTraitUseTest{

  def main(args: Array[String]): Unit = {
    val mix1 = new MixTraitUse("yxj")
    mix1.sayHello

    val mix2 = new MixTraitUse("yyy") with MyLog
    mix2.sayHello
  }

  

trait Handler {

  def handler(data: String) {}

}

trait DataValidTrait extends Handler {

  override def handler(data: String): Unit = {
    println("this is DataValidTrait handler invoked.")
    super.handler(data)
  }

}

trait SignatureTrait extends Handler {

  override def handler(data: String): Unit = {
    println("this is SignatureTrait handler invoked.")
    super.handler(data)
  }

}

class ChainPerson(name: String) extends SignatureTrait with DataValidTrait {

  def sayHello(data: String) {
    println("sayHello from " + name)
    handler(data)
  }
}

object TraitChainUse {

  def main(args: Array[String]): Unit = {
    val p1 = new ChainPerson("yxj")
    p1.sayHello("chanin data")


  }

}

  

trait Loger {
  println("loger constructor ..")
}

trait MyLoger extends Loger{
  println("MyLoger constructor...")
}

trait TimeLoger extends Loger{
  println("TimeLogers constructor...")
}

class TraitConstructor {
    println("trait constructor")
}

class SubTrait extends TraitConstructor with MyLog with TimeLoger {

  println("subtrait contructor...")

}

object tcT{

  def main(args: Array[String]): Unit = {
    val sub = new SubTrait

  }
}

  

class People(val name:String) extends Log {


  def makeFriends(p:People) ={
    println("hello, " + p.name + " i‘m " + name + ",i want to make friends with you ")
    log("makeFriends method invoked with parameter " + p.name)
  }

}


object TraitLoggerUse{

  def main(args: Array[String]): Unit = {
    val s1 = new People("yxj")
    val s2 = new People("wulanlan")

    s1.makeFriends(s2)

  }


}

  

trait HelloTrait{
  def sayHello(name:String)
}

/**
  * trait 中定义的field,继承的类,可以直接获取到使用
  *
  */
trait MakeTrait{
  val eyeNum:Int = 2
  def makeFriends(p:Person)
}

class Person(val name:String) extends HelloTrait with MakeTrait {

  def sayHello(otherName:String) = {
      println("hello " +otherName + ", im " + name + ", i have " + eyeNum + " eyes.")
  }

  def makeFriends(p: Person): Unit = {
    println("hello " + p.name + ",i‘m " + name + ",i want to make friends with you.")
  }

}

object useTrait{

  def main(args: Array[String]): Unit = {

    val p1 = new Person("yxj")
    val p2 = new Person("wulanlan")

    p1.sayHello("yy")

    p2.sayHello("zz")

    p1.makeFriends(p2)

  }

}

  

 

以上是关于scala中Trait简单使用的主要内容,如果未能解决你的问题,请参考以下文章

scala——关键字trait的使用

Scala-Trait:混入与多态

快学Scala 第十九课 (trait的abstract override使用)

Spark基础-scala学习

Scala - 特质Trait

使用 scala 构造函数设置 trait 中定义的变量