44
|
1 |
package Main
|
|
2 |
|
|
3 |
abstract class Buffer{
|
|
4 |
type T
|
|
5 |
val element: T
|
|
6 |
}
|
|
7 |
|
|
8 |
abstract class SeqBuffer extends Buffer{
|
|
9 |
type U
|
|
10 |
type T <: Seq[U]
|
|
11 |
def length = element.length
|
|
12 |
}
|
|
13 |
|
|
14 |
abstract class IntSeqBuffer extends SeqBuffer{
|
|
15 |
type U = Int
|
|
16 |
}
|
|
17 |
|
|
18 |
object AbstractTypeTest1 extends App{
|
|
19 |
def newIntSeqBuf(elem1: Int, elem2: Int): IntSeqBuffer =
|
|
20 |
new IntSeqBuffer{
|
|
21 |
type T = List[U]
|
|
22 |
val element = List(elem1, elem2)
|
|
23 |
}
|
|
24 |
val buf = newIntSeqBuf(7, 8)
|
|
25 |
println("length = " + buf.length)
|
|
26 |
println("content = " + buf.element)
|
|
27 |
}
|
|
28 |
|
|
29 |
abstract class Buffer2[+T] {
|
|
30 |
val element: T
|
|
31 |
}
|
|
32 |
|
|
33 |
abstract class SeqBuffer2[U, +T <: Seq[U]] extends Buffer2[T] {
|
|
34 |
def length = element.length
|
|
35 |
}
|
|
36 |
|
|
37 |
object AbstractTypeTest2 extends App {
|
|
38 |
def newIntSeqBuf(e1: Int, e2: Int): SeqBuffer2[Int, Seq[Int]] =
|
|
39 |
new SeqBuffer2[Int, List[Int]] {
|
|
40 |
val element = List(e1, e2)
|
|
41 |
}
|
|
42 |
val buf = newIntSeqBuf(7, 8)
|
|
43 |
println("length = " + buf.length)
|
|
44 |
println("content = " + buf.element)
|
|
45 |
}
|