点击(此处)折叠或打开
- 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)
- }
- }
-
- }
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28912557/viewspace-1985281/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/28912557/viewspace-1985281/