import java.util.ArrayList; import java.util.Random; public class Test { public static void main(String[] args) { // Holder.test(); // TupleTest.test(); // LinkedStack.test(); RandomList.test(); } } /* 泛型最初的目的是希望类或方法能够具备最广泛的表达能力。如何做到这一点,正是 通过解耦类或方法与所用的类型之间的约束。但是Java中的泛型并没有这么高的追求 */ /* 15.2 简单的泛型 */ class A{} class B{} class C{} class Holder<T> { private T a; public Holder(T a) { this.a = a; } public T getA() { return a; } public void setA(T a) { this.a = a; } public static void test() { Holder<A> h = new Holder<>(new A()); A a = h.getA(); } } /* 15.2.1 一个元祖类库 我记得STL库在实现这部分时,采用了一个很酷的写法,也是用到了继承 */ class TwoTuple<A, B> { public final A first; public final B second; public TwoTuple(A first, B second) { this.first = first; this.second = second; } public String toString() { return "(" + first + "," + second + ")"; } } class ThreeTuple<A, B, C> extends TwoTuple<A, B> { public final C third; public ThreeTuple(A first, B second, C third) { super(first, second); this.third = third; } @Override public String toString() { return "(" + first + ", " + second + ", " + third + ")"; } } class FourTuple<A,B,C,D> extends ThreeTuple{ public final D fourth; public FourTuple(Object first, Object second, Object third, D fourth) { super(first, second, third); this.fourth = fourth; } public String toString() { return "(" + first + ", " + second + ", " + third + ", " + fourth + ")"; } } class FiveTuple<A,B,C,D,E> extends FourTuple{ private E fifth; public FiveTuple(Object first, Object second, Object third, Object fourth, E fifth) { super(first, second, third, fourth); this.fifth = fifth; } public String toString() { return "(" + first + ", " + second + ", " + third + ", " + fourth + ", " + fifth + ")"; } } class TupleTest { static TwoTuple<String,Integer> f() { return new TwoTuple<>("hi", 47); } static ThreeTuple<A,String,Integer> g() { return new ThreeTuple<>(new A(), "1234", 100); } static FourTuple<String,String,String,String> h() { return new FourTuple<>("1","2","3","4"); } static FiveTuple<String,String,String,String,String> i(){ return new FiveTuple<>("1","2","3","4","5"); } static void test(){} } /* 15.2.2 一个堆栈类 */ /* 这个用的是那种逆序的链表 */ class LinkedStack<T>{ //这个地方用的时嵌套类 private static class Node<U>{ U item; Node<U> next; Node() { item = null; next = null; } Node(U item, Node next) { this.item=item; this.next=next; } boolean end() { return item == null && next==null; } } private Node<T> top = new Node<>(); 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; } static void test() { LinkedStack<String> lss = new LinkedStack<>(); for(String s : "Phasers or stun!".split(" ")) lss.push(s); String s; while ((s = lss.pop()) != null) { System.out.println(s); } } } /* 15.2.3 RandomList */ class RandomList<T>{ private ArrayList<T> storage = new ArrayList<>(); private Random random = new Random(47); public void add(T item) { storage.add(item); } public T select() { return storage.get(random.nextInt(storage.size())); } static void test() { RandomList<String> rs = new RandomList<>(); for(String s : "a b c d e f g h i j k l m n".split(" ")) rs.add(s); for (int i = 0; i < 11; i++) { System.out.println(rs.select()+" "); } } }