author | Fahad Ausaf <fahad.ausaf@kcl.ac.uk> |
Sun, 09 Nov 2014 19:25:10 +0000 | |
changeset 44 | a751aa1ee4f7 |
permissions | -rw-r--r-- |
44 | 1 |
package Main |
2 |
||
3 |
class Stack[T] { |
|
4 |
var elems: List[T] = Nil |
|
5 |
def push(x: T) { elems = x :: elems } |
|
6 |
def top: T = elems.head |
|
7 |
def pop() { elems = elems.tail } |
|
8 |
} |
|
9 |
||
10 |
object GenericsTest extends App{ |
|
11 |
val stack = new Stack[Int] |
|
12 |
stack.push(1) |
|
13 |
stack.push('a') |
|
14 |
println(stack.top) |
|
15 |
stack.pop() |
|
16 |
println(stack.top) |
|
17 |
} |