public class Gen2 { private Object ob; //定義一個通用類型成員 public Gen2(Object ob) { this.ob = ob; } public Object getOb() { return ob; } public void setOb(Object ob) { this.ob = ob; } public void showTyep() { System.out.println("T的實際類型是: " + ob.getClass().getName()); } } public class GenDemo2 { public static void main(String[] args) { //定義類Gen2的一個Integer版本 Gen2 intOb = new Gen2(new Integer(88)); intOb.showTyep(); int i = (Integer) intOb.getOb(); System.out.println("value= " + i); System.out.println("----------------------------------"); //定義類Gen2的一個String版本 Gen2 strOb = new Gen2("Hello Gen!"); strOb.showTyep(); String s = (String) strOb.getOb(); System.out.println("value= " + s); } } |
文章來源于領測軟件測試網 http://www.kjueaiud.com/