泛型

1.标识符不为基本数据类型,有自动装箱操作,Integer一切操作按数字类型进行,可避免类转换异常,若声明时未指明,则视为object类,依然正常运行

package commondy;

class Point {
    private T x;
    private T y;

    public T getX() {
        return x;
    }

    public T getY() {
        return y;
    }

    public void setX(T x) {
        this.x = x;
    }

    public void setY(T y) {
        this.y = y;
    }
}

public class demo1 {
    public static void main(String args[]) {
        Point p = new Point();
        p.setX(30);
        System.out.println(p.getX() * 2);
    }
}
package commondy;

class Point {
    private T x;

    public T getX() {
        return x;
    }
    public void setX(T x) {
        this.x = x;
    }
}

public class demo1 {
    public static void main(String args[]) {
        Point p = new Point();
        p.setX("李兴华");
        System.out.println(p.getX());
    }
}

2.通配符

可使用通配符实现泛型对象的引用传递,不能设置被泛型指定的内容,可设置null
package commondy;

class Point {
    private T x;

    public T getX() {
        return x;
    }
    public void setX(T x) {
        this.x = x;
    }
}

public class demo1 {
    public static void main(String args[]) {
        Point p = new Point();
        p.setX("李兴华");
        fun(p);
    }
    public static void fun(Point temp) {
        System.out.println(temp.getX());
    }
}

10.3泛型与子类继承的限制

子类泛型无法使用父类泛型进行接收,例如Info不能使用Info进行接收,只能用进行接收,但此时不能进行设置,可用null

10.4泛型接口

package commondy;

interface Info {
    public T getVar();
}

class InfoImpl implements Info {
    private T var;

    public InfoImpl(T var) {
        this.var = var;
    }

    public void setVar(T var) {
        this.var = var;
    }

    public T getVar() {
        return this.var;
    }
}

public class demo1 {
    public static void main(String args[]) {
        InfoImpl i = new InfoImpl("Li");
        System.out.println(i.getVar());
    }
}
package commondy;

interface Info {
    public T getVar();
}

class InfoImpl implements Info {
    private String var;

    public InfoImpl(String var) {
        this.var = var;
    }

    public void setVar(String var) {
        this.var = var;
    }

    public String getVar() {
        return this.var;
    }
}

public class demo1 {
    public static void main(String args[]) {
        InfoImpl i = new InfoImpl("Li");
        System.out.println(i.getVar());
    }
}

10.5泛型方法
泛型方法的定义与其所在的类是否无关,数据传入时的类型就是参数的类型

package commondy;

class Demo {
    public  T fun(T t) {
        return t;
    }
}

public class demo1 {
    public static void main(String args[]) {
        Demo d = new Demo();
        String str = d.fun("李兴华");
        int i = d.fun(30);
        System.out.println(str);
        System.out.println(i);
    }
}

使用泛型方法实例化

package commondy;

class Info {
    private T var;

    public T getVar() {
        return var;
    }

    public void setVar(T var) {
        this.var = var;
    }

    public String toString() {
        return this.var.toString();
    }
}

public class demo1 {
    public static void main(String args[]) {
        Info i = fun(30);
        System.out.println(i.getVar());
    }

    public static  Info fun(T tem) {
        Info temp = new Info();
        temp.setVar(tem);
        return temp;
    }
}

若传入泛型对象类型一致

package commondy;

class Info {
    private T var;

    public T getVar() {
        return var;
    }

    public void setVar(T var) {
        this.var = var;
    }

    public String toString() {
        return this.var.toString();
    }
}

public class demo1 {
    public static void main(String args[]) {
        Info i1 = new Info();
        Info i2 = new Info();
        i1.setVar("HELLO");
        i2.setVar("Li");
        add(i1, i2);
    }

    public static  void add(Info i, Info j) {
        System.out.println(i.getVar() + " " + j.getVar());

    }
}

你可能感兴趣的:(泛型)