java generic new_java中generic实例详解

一介绍:

在JavaSE1.5之前,没有泛型的情况的下,通过对类型Object的引用来实现参数的“任意化”,“任意化”带来的缺点是要做显式的强制类型转换,而这种转换是要求开发者对实际参数类型可以预知的情况下进行的。对于强制类型转换错误的情况,编译器可能不提示错误,在运行的时候才出现异常,这是一个安全隐患。

泛型的好处是在编译的时候检查类型安全,并且所有的强制转换都是自动和隐式的,提高代码的重用率。

二、泛型参数:

class Gen {

private T ob;

//定义泛型成员变量

public Gen(T ob) {

this.ob = ob;

}

public T getOb() {

return ob;

}

public void setOb(T ob) {

this.ob = ob;

}

public void showType() {

System.out.println("T的实际类型是: " + ob.getClass().getName());

}

}

public class GenericParameter {

public static void main(String[] args){

//定义泛型类Gen的一个Integer版本

Gen intOb=new Gen(100);

intOb.showType();

int i= intOb.getOb();

System.out.println("value= " + i);

System.out.println("----------------------------------");

//定义泛型类Gen的一个String版本

Gen strOb=new Gen("Hello Dylan!");

strOb.showType();

String s=strOb.getOb();

System.out.println("value= " + s);

}

}

output:

T的实际类型是: java.lang.Integer

value= 100

----------------------------------

T的实际类型是: java.lang.String

value= Hello Dylan!

三、泛型类:

class GenericsFoo {

private T x;

public GenericsFoo(T x) {

this.x = x;

}

public T getX() {

return x;

}

public void setX(T x) {

this.x = x;

}

}

public class GenericClass {

public static void main(String args[]){

GenericsFoo strFoo=new GenericsFoo("Hello Generics!");

GenericsFoo douFoo=new GenericsFoo(new double("33"));

GenericsFoo objFoo=new GenericsFoo(new Object());

System.out.println("strFoo.getX="+strFoo.getX());

System.out.println("douFoo.getX="+douFoo.getX());

System.out.println("objFoo.getX="+objFoo.getX());

}

}

output:

strFoo.getX=Hello Generics!

douFoo.getX=33.0

objFoo.getX=java.lang.Object@1d0fafc

四 限制泛型:

import java.util.ArrayList;

import java.util.Collection;

class CollectionGenFoo {

private T x;

public CollectionGenFoo(T x) {

this.x = x;

}

public T getX() {

return x;

}

public void setX(T x) {

this.x = x;

}

}

public class GenericRestrict {

public static void main(String[] args) {

CollectionGenFoo listFoo = null;

listFoo = new CollectionGenFoo(new ArrayList());

CollectionGenFoo extends Collection> listFoo1 = null;

listFoo1=new CollectionGenFoo(new ArrayList());

System.out.println("实例化成功!");

}

}

output:

实例化成功!

五 泛型方法:

public class GenericFunction {

public void f(T x) {

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

}

public static void main(String[] args) {

GenericFunction ea = new GenericFunction();

ea.f(" ");

ea.f(10);

ea.f('a');

ea.f(ea);

}

}

output:

java.lang.String

java.lang.Integer

java.lang.Character

GenericFunction

-----------------------------

dylan presents.

总结

以上就是本文关于java中generic实例详解的全部内容,希望对大家有所帮助。如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值