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

来源:互联网 发布:阿里云个人邮箱下载 编辑:程序博客网 时间:2024/06/06 09:12
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();
    }
}

0 0