天天看點

Java筆記之泛型接口

interface Demo<T> { // 定義泛型接口 
    public void print(T param); // 定義抽象方法,此方法輸出參數 
} 
  
// 實作泛型接口,方法一: 
class Imple1<T> implements Demo<T> { 
    public void print(T param) { 
        System.out.println("param = " + param); 
    } 
} 
  
//實作泛型接口,方法二: 
class Imple2 implements Demo<Imple2> { 
    public void print(Imple2 param) { 
        System.out.println("param = " + param); 
    } 
} 
  
public class GenericsDemo { 
    public static void main(String[] args) { 
        // 對以上程式進行測試 
        Imple1<String> demo = new Imple1<String>(); 
        demo.print("Hello, Wrold!"); 
          
        Imple2 im = new Imple2(); 
	        im.print(new Imple2()); 
	    } 
}