Scala的Abstract Types

trait Reader{
  type In <: java.io.Serializable //限定类型上界
  type Contents //抽象类型
  def read(in: In): Contents
}
class FileReader extends Reader {
  type In = String //具体实例化抽象类型
  type Contents = BufferedSource
  override def read(name: In) = Source.fromFile(name)
}
object Abstract_Types {

  def main(args: Array[String]) {
    val fileReader = new FileReader
    val content = fileReader.read("E:\\test.txt")
    for (line <- content.getLines){
         println(line)
        }
  }
}

你可能感兴趣的:(Scala的Abstract Types)