看个泛型的例子

package com.wjy.effective;

public class FStyle {
    public <E> E getField(E param){
        E result=param;
        return result;
    }
}

客户端程序:

package com.wjy.effective;

public class TestFStyle {
    public static void main(String args[]){
        FStyle fStyle=new FStyle();
//        int n=fStyle.getField(1)+2;
        String s=fStyle.getField("hello")+2;
        
//        System.out.println("result is; "+n);
        System.out.println("result is; "+s);
    }
}

运行结果:

//result is3
result is; hello2

猜你喜欢

转载自wjy320.iteye.com/blog/1999799