温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java中generic有什么用

发布时间:2021-07-15 11:16:45 来源:亿速云 阅读:136 作者:小新 栏目:编程语言

这篇文章将为大家详细讲解有关java中generic有什么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

一介绍:

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

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

二、泛型参数:

class Gen<T> {
	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<Integer> intOb=new Gen<Integer>(100);
		intOb.showType();
		int i= intOb.getOb();
		System.out.println("value= " + i);
		System.out.println("----------------------------------");
		//定义泛型类Gen的一个String版本
		Gen<String> strOb=new Gen<String>("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<T> {
	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<String> strFoo=new GenericsFoo<String>("Hello Generics!");
		GenericsFoo<double> douFoo=new GenericsFoo<double>(new double("33"));
		GenericsFoo<Object> objFoo=new GenericsFoo<Object>(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<T extends Collection> {
	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<ArrayList> listFoo = null;
		listFoo = new CollectionGenFoo<ArrayList>(new ArrayList());
		CollectionGenFoo<? extends Collection> listFoo1 = null;
		listFoo1=new CollectionGenFoo<ArrayList>(new ArrayList());
		System.out.println("实例化成功!");
	}
}

output:
实例化成功!

五 泛型方法:

public class GenericFunction {
	public <T> 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有什么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI