请看代码//: generics/LinkedStack.java// A stack implemented with an internal linked structure.package generics;public class LinkedStack<T> { private static class Node { T item; Node next; Node() { item = null; next = null; } Node(T item, Node next) { this.item = item; this.next = next; } boolean end() { return item == null && next == null; } } private Node top = new Node(); // End sentinel public void push(T item) { top = new Node(item, top); } public T pop() { T result = top.item; if(!top.end()) top = top.next; return result; } public static void main(String[] args) { LinkedStack<String> lss = new LinkedStack<String>(); for(String s : "Phasers on stun!".split(" ")) lss.push(s); String s; while((s = lss.pop()) != null) System.out.println(s); }} /* Output:stun!onPhasers*///:~
代码很简单,就是一对编译错误,我想让内部类Node直接访问模板参数T,而不是像这样声明Node<U>,请问应该怎么做呢?试了好几次都没成功,请大家不吝指教。
代码很简单,就是一对编译错误,我想让内部类Node直接访问模板参数T,而不是像这样声明Node<U>,请问应该怎么做呢?试了好几次都没成功,请大家不吝指教。
泛型在运行时才能知道对象类型
你不告诉类这个是泛型,它怎么给你处理那