Scala Interface

Scala Interface Description

Here Insert Picture Description

Scala Interface affirmed

Here Insert Picture Description

Case

package test2

object TeaitDemo2 {
  def main(args: Array[String]): Unit = {
    val c = new C()
    c.aWay1()
    c.getConnec()

    val f = new F()
    f.getConnec()
  }
}


trait Trait01 {
  def getConnec()
}

class A {
  def aWay1() = {
    println("a away")
  }
}

class B extends A {

}

class C extends A with Trait01 {
  override def getConnec(): Unit = {
    println("连接mysql...")
  }
}

class D {

}

class E extends D {

}

class F extends D with Trait01 {
  override def getConnec(): Unit = {
    println("连接oracle...")
  }
}

Here Insert Picture Description

package test3

object TraitDemo3 {
  def main(args: Array[String]): Unit = {
    val sheep = new Sheep
    sheep.sayHi()
    sheep.sayHello()
  }
}

trait Trait03 {

  // 抽象方法
  def sayHi()

  // 实现普通方法
  def sayHello() = {
    println("say hello ...")
  }
}

class Sheep extends Trait03 {
  override def sayHi(): Unit = {
    println("小绵羊 喕喕喕喕喕")
  }
}

Here Insert Picture Description

package test4

object MixInDemo {
  def main(args: Array[String]): Unit = {
    val oracleDB = new OracleDB with Operate3
    oracleDB.insert(100)

    val mysql = new Mysql with Operate3
    mysql.insert(200)

    val mysql_ = new Mysql_ with Operate3 {
      override def say(): Unit = {
        println("say hi")
      }
    }
    mysql_.insert(999)
    mysql_.say()
  }
}

trait Operate3 {
  def insert(id:Int) = {
    println("插入数据 = " + id)
  }
}

class OracleDB {

}

abstract class Mysql {

}

abstract class Mysql_ {
  def say()
}

Here Insert Picture Description

package test5

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

    val mySQL4 = new MySQL4 with DB4 with File4
    mySQL4.insert(100)

  }
}

trait Operate4 {
  def insert(id:Int)
}

trait Data4 extends Operate4 {
  println("Data4")

  override def insert(id: Int): Unit = {
    println("插入数据:"+ id)
  }
}

trait DB4 extends Data4 {
  println("DB 4")

  override def insert(id: Int): Unit = {
    println("向数据库")
    super.insert(id)
  }
}

trait File4 extends Data4 {
  println("File4")

  override def insert(id: Int): Unit = {
    println("向文件")
    super.insert(id)
  }
}


class MySQL4 {

}

Here Insert Picture Description

Note superposition characteristics

Here Insert Picture Description

Rich Interface

The interface in both abstract methods have non-abstract method

package test6

trait Operate {
  def insert (id:Int)

  def pageQuery(start:Int,end:Int) = {
    println("分页查询")
  }

}

package test6

class MyTest extends Operate {
  override def insert(id: Int): Unit = {
    println("实现方法")
  }

  override def pageQuery(start: Int, end: Int): Unit = {
    println("重新非抽象方法")
  }
}

package test6

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

    val test1 = new MyTest
    test1.insert(666)
    test1.pageQuery(0,10)
  }
}

Here Insert Picture Description

The specific nature of the field

Here Insert Picture Description

package test7

object MixInPro {
  def main(args: Array[String]): Unit = {
    val mySQL6 = new MySQL6 with DB6 {
      override var sal: Int = 100
    }
    println(mySQL6.sal)
    println(mySQL6.opertype)
  }
}

trait DB6 {
  var sal:Int
  var opertype:String = "insert"

  def insert() = {

  }
}

class MySQL6 {

}

Here Insert Picture Description

Their type

Here Insert Picture Description

package test8

class MyBean {
  def sayHi(): Unit ={
    println("hi")
  }
}
package test8

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

  }
}

trait Logger {
  this:Exception =>
  def log() = {
    println(getMessage)
  }
}
package test8

object SelfTest {
  def main(args: Array[String]): Unit = {
    val student = new Student
    student.sayHi()
  }
}

trait MyTest {
  this:MyBean=>
  def say() = {
    this.sayHi()
  }
}

//class S extends MyTest {}  语法错误

class Student extends MyBean with MyTest {

}

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/chen18677338530/article/details/91492684
Recommended