泛型之类使用了泛型的示例

package fft.generics;

//表示ship的类型继承了number的字段
public class Ship<T extends Number> {
    private  T  t;
    
    public Ship(T t){
        this.t=t;
    }
    
    public void print(){
        
        System.out.println(t.getClass().getName());//打印出这个方法的名称
        
        
    }
    public static void main(String[] args) {
        Ship<Integer> s1 = new Ship<Integer>(3);
        s1.print();
        Ship<Float> s2 = new Ship<Float>(new Float(3.26));
        s2.print();
        Ship<Double> s3 = new Ship<Double>(new Double(3.26987));
        s3.print();
    }
}

发布了195 篇原创文章 · 获赞 52 · 访问量 31万+

猜你喜欢

转载自blog.csdn.net/hongweideng/article/details/53840561