Scala编程

Scala编程详解12:面向对象编程之Trait

2020-05-26  本文已影响0人  勇于自信

大纲
1、trait基础知识
1-1 将trait作为接口使用
1-2 在trait中定义具体方法
1-3 在trait中定义具体字段
1-4 在trait中定义抽象字段
2、trait高级知识
2-1 为实例对象混入trait
2-2 trait调用链
2-3 在trait中覆盖抽象方法
2-4 混合使用trait的具体方法和抽象方法
2-5 trait的构造机制
2-6 trait字段的初始化
2-7 让trait继承类

1. 将trait作为接口使用

object scala_demo09 {
  def main(args: Array[String]): Unit = {
    val person = new Person("lisi")
    person.sayHello("zhangsan")
    val person2 = new Person("wangwu")
    person.makeFriends(person2)
  }
}
trait HelloTrait {
  def sayHello(name: String)
}
trait MakeFriendsTrait {
  def makeFriends(p: Person)
}
class Person(val name: String) extends HelloTrait with MakeFriendsTrait with Cloneable with Serializable {
  def sayHello(name: String) = println("Hello, " + name)
  def makeFriends(p: Person) = println("Hello, my name is " + name + ", your name is " + p.name)
}

输出:

Hello, zhangsan
Hello, my name is lisi, your name is wangwu
2. 在Trait中定义具体方法

object scala_demo09 {
  def main(args: Array[String]): Unit = {
    val p = new Person("zhangsan")
    val p2 = new Person("lisi")
    p.makeFriends(p2)
  }
}
trait Logger {
  def log(message: String) = println(message)
}

class Person(val name: String) extends Logger {
  def makeFriends(p: Person) {
    println("Hi, I'm " + name + ", I'm glad to make friends with you, " + p.name)
    log("makeFriends methdo is invoked with parameter Person[name=" + p.name + "]")
  }
}

输出:

Hi, I'm zhangsan, I'm glad to make friends with you, lisi
makeFriends methdo is invoked with parameter Person[name=lisi]
3. 在Trait中定义具体字段

object scala_demo09 {
  def main(args: Array[String]): Unit = {
    val student = new Student("zhangsan")
    student.sayHello
  }
}
trait Person {
  val eyeNum: Int = 2
}

class Student(val name: String) extends Person {
  def sayHello = println("Hi, I'm " + name + ", I have " + eyeNum + " eyes.")
}

输出:

Hi, I'm zhangsan, I have 2 eyes.
4. 在Trait中定义抽象字段

object scala_demo09 {
  def main(args: Array[String]): Unit = {
    val p = new Person("zhangsan")
    val p2 = new Person("lisi")
    p.makeFriends(p2)

  }
}
trait SayHello {
  val msg: String
  def sayHello(name: String) = println(msg + ", " + name)
}

class Person(val name: String) extends SayHello {
  val msg: String = "hello"
  def makeFriends(p: Person) {
    sayHello(p.name)
    println("I'm " + name + ", I want to make friends with you!")
  }
}

输出:

hello, lisi
I'm zhangsan, I want to make friends with you!
5. 为实例混入trait

object scala_demo09 {
  def main(args: Array[String]): Unit = {
    val p1 = new Person("leo")
    p1.sayHello
    val p2 = new Person("jack") with MyLogger
    p2.sayHello

  }
}
trait Logged {
  def log(msg: String) {}
}
trait MyLogger extends Logged {
  override def log(msg: String) {
    println("log: " + msg)
  }
}
class Person(val name: String) extends Logged {
  def sayHello {
    println("Hi, I'm " + name); log("sayHello is invoked!")
  }
}

输出:

Hi, I'm leo
Hi, I'm jack
log: sayHello is invoked!
6. trait调用链

object scala_demo09 {
  def main(args: Array[String]): Unit = {
    val p1 = new Person("leo")
    p1.sayHello

  }
}
trait Handler {
  def handle(data: String) {}
}
trait DataValidHandler extends Handler {
  override def handle(data: String) {
    println("check data: " + data)
    super.handle(data)
  }
}
trait SignatureValidHandler extends Handler {
  override def handle(data: String) {
    println("check signature: " + data)
    super.handle(data)
  }
}
class Person(val name: String) extends SignatureValidHandler with DataValidHandler {
  def sayHello = { 
    println("Hello, " + name) 
    handle(name) 
  }
}

输出:

Hello, leo
check data: leo
check signature: leo
7. 在trait中覆盖抽象方法
trait Logger {
  def log(msg: String)
}

trait MyLogger extends Logger {
  abstract override def log(msg: String) { super.log(msg) }
}
8. 混合使用trait的具体方法和抽象方法
trait Valid {
  def getName: String
  def valid: Boolean = {
    getName == "leo"    
  }
}
class Person(val name: String) extends Valid {
  println(valid)
  def getName = name
}
9. trait的构造机制

object scala_demo09 {
  def main(args: Array[String]): Unit = {
    val student = new Student
  }
}
class Person { println("Person's constructor!") }
trait Logger { println("Logger's constructor!") }
trait MyLogger extends Logger { println("MyLogger's constructor!") }
trait TimeLogger extends Logger { println("TimeLogger's constructor!") }
class Student extends Person with MyLogger with TimeLogger {
  println("Student's constructor!")
}

输出:

Person's constructor!
Logger's constructor!
MyLogger's constructor!
TimeLogger's constructor!
Student's constructor!
10. trait field的初始化
trait SayHello {
  val msg: String
  println(msg.toString)
}

class Person
val p = new {
  val msg: String = "init"
} with Person with SayHello

class Person extends {
  val msg: String = "init"
} with SayHello {}

// 另外一种方式就是使用lazy value
trait SayHello {
  lazy val msg: String = null
  println(msg.toString)
}
class Person extends SayHello {
  override lazy val msg: String = "init"
}
11. trait继承class

object scala_demo09 {
  def main(args: Array[String]): Unit = {
    val p = new Person("zhangsan")
    p.sayHello
  }
}

class MyUtil {
  def printMessage(msg: String) = println(msg)
}

trait Logger extends MyUtil {
  def log(msg: String) = printMessage("log: " + msg)
}

class Person(val name: String) extends Logger {
  def sayHello {
    log("Hi, I'm " + name)
    printMessage("Hi, I'm " + name)
  }
}

输出:

log: Hi, I'm zhangsan
Hi, I'm zhangsan
上一篇下一篇

猜你喜欢

热点阅读