模板类和模板方法的使用
public class Coffee { static class Box<T>{ private T contents; public Box(T contents){ this.contents=contents; } public T getContents(){ return contents; } public void setContents(T contents){ this.contents=contents; } } public static void main(String[] args) { //模板类 Box<String> StringBox=new Box<String>("hello"); System.out.println(StringBox.getContents()); Box IntergerBox=new Box<Integer>(5); System.out.println(IntergerBox.getContents()); System.out.println("---------------------"); //模板方法 TemplateMethod("123"); TemplateMethod(6); TemplateMethod(8.8); } public static <K> void TemplateMethod(K k){ System.out.println(k); } }