【泛型】java泛型之自定义泛型extends,super

自定义泛型:extends,super关键字的使用

1.泛型类的使用		public class ResponseResult<T> {}
2.泛型方法的使用	    public static <T extends String> T concat(T... ts) {}
3.泛型接口的使用		public interface StringBufferInterface<T extends String> {}
4.泛型关键字介绍		Fan<?>Fan<? extends A>Fan<? super A1>Fan<Object>

1.泛型类的使用:比较常见的接口响应数据封装 ResponseResult.java

package pers.li.genericity.$1genericclass.practise;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ResponseResult<T> {
    private T data;
    private String msg;
    private int code;
    public ResponseResult(T data, String msg, int code) {
        this.data = data;
        this.msg = msg;
        this.code = code;
    }

    public ResponseResult(T data) {
        this.code = 200;
        this.data = data;
    }
    public ResponseResult() {
        this.code = 200;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public static void main(String[] args) throws JsonProcessingException {
        ResponseResult<String> stringResponseResult = new ResponseResult<>();
        System.err.println(new ObjectMapper().writeValueAsString(stringResponseResult));
    }
}

2.泛型方法的使用:简单示例字符串拼接

package pers.li.genericity.$1genericmethod.practise;
public class dataDeal {

    public static <T extends String> T concat(T... ts) {
        StringBuffer stringBuffer = new StringBuffer();
        for (String s:ts
             ) {
            stringBuffer.append(s);
        }
        return (T) stringBuffer.toString();
    }

    public static void main(String[] args) {
        System.err.println(dataDeal.concat("1","2"," ","3"));
    }
}

3.泛型接口的使用:简单示例字符串拼接

package pers.li.genericity.$1genericinterface.practise;
public interface StringBufferInterface<T extends String> {
    public T concat (T... t2);
}

class TestStringBuffer implements StringBufferInterface<String>{
    @Override
    public String concat(String... t2) {
        StringBuffer stringBuffer = new StringBuffer();
        for (String s:t2
             ) {
            stringBuffer.append(s);
        }
        return stringBuffer.toString();
    }

    public static void main(String[] args) {
        TestStringBuffer test = new TestStringBuffer();
        String add = test.concat("1","2");
        StringBufferInterface<String> test2 =  new TestStringBuffer();
        String add2 = test2.concat("1","2");
        System.err.println(add+"|"+add2);
    }
}

4.泛型关键字对比介绍:

package pers.li.genericity.$2limits.practise;

//定义父类
abstract class A {
    private int s;

    public int getS() {
        return s;
    }

    public void setS(int s) {
        this.s = s;
    }

    public abstract void thisName();
}

//定义子类A1
class A1 extends A {

    @Override
    public void thisName() {
        System.err.println("this is A1");
    }
}
//定义A1的子类A11
class A11 extends A1 {
    @Override
    public void thisName() {
        System.err.println("this is A11");
    }
}
//定义子类A2
class A2 extends A {
    @Override
    public void thisName() {
        System.err.println("this is A2");
    }
}

//定义泛型类
class Fan<T>{
    private T fan1;
    private T fan2;

    public T getFan1() {
        return fan1;
    }

    public void setFan1(T fan1) {
        this.fan1 = fan1;
    }

    public T getFan2() {
        return fan2;
    }

    public void setFan2(T fan2) {
        this.fan2 = fan2;
    }

    public Fan(T fan1, T fan2) {
        this.fan1 = fan1;
        this.fan2 = fan2;
    }
    public Fan() {
        this.fan1 = null;
        this.fan2 = null;
    }
}

public class Test {

    public static void main(String[] args) {
    	//?号表示任意类型,即Object类型,所以取值需要强转才能调用方法,放值(即set方法)由于不确定类型,所以会报错
        Fan<?> fan = new Fan(new A1(),new A2());
        Object fan1 = fan.getFan1();
        A1 a = (A1)fan1;
        a.thisName();
//        fan.setFan1(new A1());
        System.err.println("? super A1只能存放A1及其子类对象并且不能get,因为不知道放进去的对象是哪个+++++++++++++++++++++++++++++===");
        //只能存放A1及其子类对象并且不能get,因为不知道放进去的对象是哪个,可以通过强制转换获取结果
        Fan<? super A1> fan2 = new Fan<A>();
        fan2.setFan1(new A1());
        fan2.setFan2(new A11());
        Object fan11 = fan2.getFan1();
        A1 ss = (A1)fan11;
        ss.thisName();
//        A fan11 = fan2.getFan1();
        System.err.println("? extends A只能取值不能放值,因为T必须是继承于A的子类,放值时无法知道到底是哪个子类+++++++++++++++++++++++++++++===");
        Fan<? extends A> fan3 = new Fan(new A1(),new A2());
        fan3.getFan1().thisName();
        fan3.getFan2().thisName();
       /* fan3.setFan2(new A1());
        fan3.setFan2(new A2());
        fan3.setFan2(new A() {
            @Override
            public void thisName() {

            }
        });*/
        System.err.println("真实泛型========================================");
        Fan<A> fan4 = new Fan(new A1(),new A2());
        fan4.getFan1().thisName();
        fan4.getFan2().thisName();
        fan4.setFan2(new A2());
    }


}

你可能感兴趣的:(java,开发语言)