`

看个泛型的例子

 
阅读更多
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

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics