天天看点

java之泛型(Generic)

泛型的复习

一、认识泛型

java之泛型(Generic)
public class GenericText{

    //泛型的简单实用,
    public static void main(String[] args) {
        //根据你自己的需求可以变换你需要的类型,我下面用String类型
        Genericdemo<String> gd = new Genericdemo<>();
        gd.setX("10");
        gd.setY("20");
        System.out.println(gd.getX()+"---"+gd.getY());
    }


    static class Genericdemo<T>{
        public T x;
        public T Y;


        //写一个构造方法,来接通主类

        public T getX() {
            return x;
        }


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


        public T getY() {
            return Y;
        }


        public void setY(T y) {
            Y = y;
        }
    }
}
           

打印结果:

java之泛型(Generic)

二、如何在构造方法中使用泛型

java之泛型(Generic)
public class GenericText01<T>{

    public static void main(String[] args) {
        //根据你自己的需求可以变换你需要的类型,我下面用String类型
        Genericdemo<String> gd = new Genericdemo<>("我在构造方法中使用了泛型");
        System.out.println(gd.getValue());
    }
    //我们在公共类使用单个泛型
    static class Genericdemo<T>{
        public T value;
        //构造方法中直接用T来代替即可
        public Genericdemo(T value) {
            super();
            this.value = value;
        }

        public T getValue() {
            return value;
        }

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

打印结果:

java之泛型(Generic)

三、如何使用多个泛型

java之泛型(Generic)
public class GenericText01{

    public static void main(String[] args) {
        //根据你自己的需求可以变换你需要的类型,我下面用String类型
        Genericdemo<String,Integer> gd = new Genericdemo<>("我在构造方法中使用了泛型",);
        System.out.println(gd.getValue()+"----"+gd.getKey());
    }
    //我们在公共类使用多个泛型
    static class Genericdemo<T,k>{
        public T value;
        public k key;
        //构造方法中直接用T来代替即可
        public Genericdemo(T value) {
            super();
            this.value = value;
        }

        public T getValue() {
            return value;
        }


        public Genericdemo(T value, K key) {
            super();
            this.value = value;
            this.key = key;
        }

        public K getKey() {
            return key;
        }

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

打印结果:

java之泛型(Generic)

四、泛型中的通配符,用于接受各种类型

public class GenericText02<T>{

    public static void main(String[] args) {
        //根据你自己的需求可以变换你需要的类型,我下面用String类型
        Genericdemo<String> gd = new Genericdemo<>();
        gd.setValue("我在测试通配符");

        tell(gd);

        //然后我们把gd.getValue()传递给一个方法
    }

    /*
     * 这里面<?>,里面的“?”就是通配符,不管你上面传递过来的什么类型,他都可以接受
     * */
    public static void tell(Genericdemo<?> gd){
        System.out.println("我是传递过来的值,可以是任何类型: "+gd);
    }


    //我们在公共类使用单个泛型
    static class Genericdemo<T>{
        public T value;

        public T getValue() {
            return value;
        }
        public void setValue(T value) {
            this.value = value;
        }
        @Override
        public String toString() {
            return this.getValue().toString();
        }


    }
}
           

打印结果:

java之泛型(Generic)

五、在接口中使用泛型

java之泛型(Generic)
//1、我们先写一个接口,使用泛型,定义一个方法
    interface style<T>{
        public void getstyle();
    }


    //我们写一个接口的子类,实现我们定义的接口,里面的类型自己去定义
    class TTT implements style<String>{
        public String valueString;

        public String getValueString() {
            return valueString;
        }

        public void setValueString(String valueString) {
            this.valueString = valueString;
        }

        public TTT(String valueString) {
            super();
            this.valueString = valueString;
        }
        //没有用到这个方法
        @Override
        public void getstyle() {

        }
    }

public class GenericText04<T>{
    //我们写一个接口的子类,实现我们定义的接口
    //泛型的简单实用,
    public static void main(String[] args) {
        //创建我们子类对象
        TTT gd = new TTT("我在这里用了接口泛型");
        System.out.println(gd.getValueString());
    }
}
           

打印结果:

java之泛型(Generic)

六、泛型方法的使用

java之泛型(Generic)
public class GenericText03<T>{

    //泛型方法
    public static void main(String[] args) {
        //根据你自己的需求可以变换你需要的类型,我下面用String类型
        Genericdemo<String> gd = new Genericdemo<>();
        String s = gd.tell("A");
        System.out.println("我传的是字符"+s);

        int num = gd.tell();
        System.out.println("我传的是数字"+num);
    }
    static class Genericdemo<T>{

        public <T> T tell(T t){
            return t;
        }
    }
}
           

打印结果:

java之泛型(Generic)

六、数组使用泛型,就几句

java之泛型(Generic)