Scala 範例 - 抽象型態 (abstractTypes)

原始程式:abstractTypes.scala

object abstractTypes extends Application {
  abstract class Buffer {
    type T; val element: T
  }
  abstract class SeqBuffer {
    type T; val element: Seq[T]; def length = element.length
  }
  def newIntBuffer(el: Int) = new Buffer {
    type T = Int; val element = el
  }
  def newIntBuffer(el: Int*) = new SeqBuffer {
    type T = Int; val element = el
  }
  println(newIntBuffer(1).element)
  println(newIntBuffer(1, 2, 3).length)
}

執行結果:

D:\Scala\code>scala abstractTypes
1
3
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License