Swift-----泛型Generic
struct Stack<T> { var items = Array<T>() func isEmpty() -> Bool { return items.count == 0 } mutating func push(value: T) { items.append(value) } mutating func pop() -> T? { return items.removeLast() } }
// 对Stack进行扩展 extension Stack { var top: T? { return items.last } } var stack = Stack<Int>() stack.push(value: 1) stack.push(value: 2) stack.pop() var s = Stack<String>() s.push(value: "Hello") s.pop() s.top