11级_Java_曹建波4.27 Java中的泛型编程

Java中的泛型编程

泛型是JDK1.5后出现的一个安全机制

好处:1.将运行时期出现的问题转移到编译时期。

2.避免了强制类型转换的麻烦

通过<>来指定容器中元素的类型

泛型的思想
消除取用集合元素时代码中的强制类型转换,比如事先规定好一个集合中允许加入的具体元素类型,然后在编译环节实现集合中添加元素的类型检查,以防止有人将非预期类型的元素保存到集合中。

优点
类型错误可以在编译时暴露出来,而不是在运行时才发作(抛ClassCastException),这有助于早期错误排查,并提高程序的可靠性。

使用泛型的Vector

java.util包里的Vector类的泛型声明:

publicclassVector<E>{

voidadd(Ex);

….

}

Vector<Integer>c=newVector<Integer>();

c.add(15);c.add(newInteger(10));

//c.add("name");

for(inti=0;i<c.size();i++){

Integervalue=c.get(i);System.out.println(value);

}

注意:

1JDK5.0后集合类定义像Vector一样都进行了泛型化改造,还有泛型类带有多个类型参数,如Map<K,V>就有两个类型参数,表示键-值映射。

2Java语言中的泛型是维护向后兼容的,即我们完全可以不采用泛型,而继续沿用过去的做法。

3)在高版本开发环境中编译未启用泛型机制的集合类应用代码时,会输出编译提示信息。

当类中操作的引用数据类型不能确定是,就使用泛型类。

类中要操作什么类型的数据不确定,就可以定义一个类型参数。

使用者,传递什么样的类型,那么该类中操作的就是什么类型数据。

classStudent<T>

{ privateStringname;

privateTt;

publicStudent(Stringname,Tt){

this.name=name;

this.t=t;

}

publicTgetT(){

returnt;

}

}

classTestStu{

publicstaticvoidmain(String[]args){

Student<String>stu1=

newStudent<String>("张三","软件学院");

Student<Double>stu2=

newStudent<Double>("李四",95.0);

Stringaddress=stu1.getT();

Doubled=stu2.getT();

System.out.println(address);

System.out.println(d);

}

}

classTestGeneric

{

publicstaticvoidmain(String[]args){

Vector<String>c=newVector<String>();

c.add("aaa");

Vector<Integer>v=newVector<Integer>();

v.add(123);

printC(c);

printC(v);}

staticvoidprintC(Vector<?>c){

Iterator<?>it=c.iterator();

while(it.hasNext()){

System.out.println(it.next());

}

}

}

为了更好的在定义泛型类时对类型参数进行限制,泛型机制还允许对类型参数进行附加的约束。

<TextendsPerson>

类型参数为Person类或者Person类的子类

classPerson

{ publicvoidp(){

System.out.println("Person");

}

}

classStudentextendsPerson

{ publicvoidp(){

System.out.println("student");

}

}

classTeacherextendsPerson

{ publicvoidp(){

System.out.println("teacher");

}

}

classTestGeneric2<TextendsPerson>

{privateTt;

publicTestGeneric2(Tt){

this.t=t;

}

publicvoidtestp(){

t.p();}

publicstaticvoidmain(String[]args){

TestGeneric2<Person>tg2=

newTestGeneric2<Person>(newPerson());

tg2.testp();

TestGeneric2<Student>tg3=

newTestGeneric2<Student>(newStudent());

tg3.testp();

TestGeneric2<Teacher>tg4=

newTestGeneric2<Teacher>(newTeacher());

tg4.testp(); }

}

泛型方法:就是将泛型定义在方法上。

泛型定义在类上,该泛型作用于整个类。

将泛型定义在方法上即可让使用泛型的方法操作不同类型数据。

格式:<T>返回类型方法名(Tt)

classTestGeneric3

{

public<T>voidgtest(Tt){

System.out.println(t.getClass().getName());

}

publicstaticvoidmain(Stringargs[]){

TestGeneric3tg=newTestGeneric3();

tg.gtest("");

tg.gtest(tg);

tg.gtest(123);

}

}

静态方法不可以使用类中定义的泛型。

因为类中的泛型需要在对象初始化的时,指定具体类型。

而静态优先与对象存在。

在静态方法上定义泛型,一定要写在static关键字的后面。返回值类型的前边。

classUtil<T>

{

publicvoiddisplay(Tt){

System.out.println(t);

}

}

classTestGeneric4

{

publicstaticvoidmain(String[]args){

Util<String>u=newUtil<String>();

u.display("abc");

}

}

classUtil

{

publicstatic<T>voiddisplay(Tt){

System.out.println(t);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值