Java泛型

定义

(①)基本概念:

1.泛型变量(type variable)

JSL中有如下三句话
1)There are four kinds of  reference types: class types ( §8.1), interface types ( §9.1), type variables ( §4.4), and array types ( §10.1).
2)A  type variable is an unqualified identifier used as a type in class, interface, method, and constructor bodies.
3)A type variable is introduced by the declaration of a  type parameter of a generic class, interface, method, or constructor.

可以看出,type variable被当做一种引用类型在java中使用。
通过定义泛型类、接口、方法、构造函数,引入泛型变量。则在该泛型变量的作用范围为, 该标识符可以被当做某种 “引用类型” 使用


既然是变量,则有scope概念
泛型类:
1)the type parameter section of the class declaration
2)the type parameter section of any superclass or superinterface of the class declaration
3)the class body.
泛型接口: 
1)the type parameter section of the interface declaration
2)the type parameter section of any superinterface of the interface declaration
3)the interface body.
泛型方法:the entire declaration of the method, including the type parameter section, but excluding the method modifiers.
泛型构造器:the entire declaration of the constructor, including the type parameter section, but excluding the constructor modifiers.

2.泛型(generics)【广义的泛型,可以理解为泛型技术】
Generics allow you to abstract over types,模板类型概念?

3.泛型(generic type)【狭义的泛型,可以理解为使用泛型技术的type】
JDK5之前,type:class type、interface type、array type
故generic type可以理解为:对JDK5之前的类型,应用泛型技术generics,得新类型:泛型类、泛型接口、泛型数组(因为类型擦除等Java中无真正的泛型数组)
细究无益:可以片面的理解为在声明类、接口时,引入类型变量用作 类型形参。如:JDK中List<E>的定义

4.参数化类型(parameterized type)
A parameterized type is a class or interface type of the form  C < T1,..., Tn >, where  C is the name of a generic type and  < T1,..., Tn > is a list of type arguments that denote a particular  parameterization of the generic type.
可以理解为,generic type的实际使用,传入类型实参后所得的类型。如,List<Integer> list;

5.原生类型(raw type)
对于在声明时引入类型变量的类或接口,在实际使用时不传入类型实参,将其当做JDK5之前的类型使用。如,List list;


(②)泛型类、泛型接口、泛型方法、泛型构造函数

interface MyList<E,T extend E> extend List<E>{}
可以看出这里对于泛型接口 MyList,有type variable  E,分别用在了type parameter section(T extend E)、type parameter section of superinterface(List <E>)
泛型类、泛型接口、泛型方法、泛型构造函数的声明见~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值