泛型

作者: CHENpq | 来源:发表于2019-05-05 23:41 被阅读0次

     Generic

    Note that type parameters can represent only reference types, not primitive types (like int, double and char).

    只能是引用类型,不能是基本类型

    范型的通配符问题

    The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments.

    <?>

    //有约束的通配

    <? extends List>

    <? super ArrayList>

    泛型解决的是向下转型所带来的安全隐患,其核心的组成就是在声明类<>

    public class WildCard<T> {

        private T foo;

        public T getFoo() {return foo;}

        public void setFoo(T foo) {this.foo = foo;}

        public static void main(String[] args) {

            WildCard<?> ge1 = new WildCard<>();

            WildCard<String> ge2 = new WildCard<>();

        }

    }

    “?”可以接收任意的泛型类型,只能够取出,但是不能修改。

    相关文章

      网友评论

          本文标题:泛型

          本文链接:https://www.haomeiwen.com/subject/ajfloqtx.html