Java泛型二:泛型类 泛型接口 泛型方法详解

泛型类

单参数泛型类:

public class Rectangle<T> {
    private T width;
    private T height;

    public Rectangle(T width, T height){
        this.width = width;
        this.height = height;
    }

    public T getWidth() {
        return width;
    }

    public void setWidth(T width) {
        this.width = width;
    }

    public T getHeight() {
        return height;
    }

    public void setHeight(T height) {
        this.height = height;
    }

    public static void main(String [] args){
        Integer width = 10;
        Integer height = 5;
        Rectangle<Integer> rect = new Rectangle<Integer>(width, height);

        String widthStr = "20";
        String  heightStr = "40";
        Rectangle<String> rect1 = new Rectangle<String>(widthStr, heightStr);
    }
}

通过public class Rectangle<T> {}定义泛型类,在实例化该类时,必须指明泛型T的具体类型,例如:Rectangle<String> rectangle = new Rectangle<String>();,指明泛型T的类型为String。

多参数泛型类:

public class Container<K, V> {
    private K key;
    private V value;

    public Container(K k, V v) {
        key = k;
        value = v;
    }

    public K getKey() {
        return key;
    }

    public void setKey(K key) {
        this.key = key;
    }

    public V getValue() {
        return value;
    }

    public void setValue(V value) {
        this.value = value;
    }

    public static void main(String[] args) {
        Container<String, String> c1 = new Container<String, String>("name", "Messi");
        Container<String, Integer> c2 = new Container<String, Integer>("age", 30);
        Container<String, Double> c3 = new Container<String, Double>("height", 1.78);
        System.out.println(c1.getKey() + " : " + c1.getValue());
        System.out.println(c2.getKey() + " : " + c2.getValue());
        System.out.println(c3.getKey() + " : " + c3.getValue());
    }
}

通过public class Container<K, V> {}定义泛型类,在实例化该类时,必须指明泛型K、V的具体类型,例如:Container<String, String> c1 = new Container<String, String>("name", "Messi");,指明泛型K的类型为String,泛型V的类型为String。

泛型接口

public interface Calculator<T> {
    public T and(T a, T b);
}
public class CalculatorInteger implements Calculator<Integer>{
    public Integer and(Integer a, Integer b){
        return a + b;
    }

    public static void main(String[] args) {
        CalculatorInteger ci = new CalculatorInteger();
        Integer val = ci.and(10, 20);
        System.out.println(val);
    }
}
public class CalculatorString implements Calculator<String>{
    public String and(String a, String b){
        return a + b;
    }

    public static void main(String[] args) {
        CalculatorString ci = new CalculatorString();
        String val = ci.and("10", "20");
        System.out.println(val);
    }
}

通过public interface Calculator<T> {}定义泛型接口,在实现该接口时,必须指明泛型T的具体类型,例如:public class CalculatorInteger implements Calculator<Integer>{},指明泛型T的类型为Integer,实例化该类时无需制定泛型类型。

泛型方法

public class Generic {
    public <T> T getObject(Class<T> c){
        T t = null;
        try {
            t = c.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    public static void main(String [] args){
        try {
            Generic generic = new Generic();
            Object obj = generic.getObject(Class.forName("com.ips.volatiles.demo2.Generic"));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

泛型方法说明:
这里写图片描述

在调用泛型方法的时候,在不指定泛型的情况下,泛型变量的类型为该方法中的几种类型的同一个父类的最小级,直到Object。在指定泛型的时候,该方法中的几种类型必须是该泛型实例类型或者其子类。

在初始化泛型类的时候,不指定泛型的时候,泛型的类型为Object,就比如ArrayList中,如果不指定泛型,那么这个ArrayList中可以放任意类型的对象。

泛型的好处是在编译的时候进行类型安全检查,并且所有的强制转换都是自动和隐式的,以提高代码的重用率。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值