您的位置:首页 > 编程语言 > Java开发

java泛型(一)基本介绍和使用

2017-05-25 00:00 489 查看
摘要: java泛型之基础介绍和使用

在面向对象编程语言中,多态算是一种泛化机制。一般的类和方法,只能使用具体的类型:要么是基本类型,要么是自定义的类。如果要编写可以应用多种类型的代码,这种刻板的限制对代码的约束就会很大。接下来我们先来看看一个只能持有单个对象的类。

简单泛型

class Automobile{}
public class Holder1 {
public Automobile aa;
public Holder1(Automobile aa){ this.aa = aa; }
Automobile get(){ return aa; }

public static void main(String[] args) {
Holder1 holder1 = new Holder1(new Automobile());
Automobile automobile = holder1.get();
System.out.println(automobile);
}
}

实现完了,但是这个类的可重用性就不怎样了,它无法持有其他类型的任何对象。我们可不希望为碰到的每一个类型都编写一个新的类。在java SE5之前,我们可以让这个类直接持有Object类型的对象。

public class Holder2 {
private Object aa;
public Holder2(Object aa){ this.aa = aa; }
public void set(Object aa){ this.aa = aa; }
public Object get(){ return aa; }

public static void main(String[] args) {
Holder2 holder2 = new Holder2(new Automobile());
Automobile automobile = (Automobile) holder2.get();
holder2.set("is 张三,NOT IS Automobile");
String str = (String) holder2.get();
holder2.set(1);
Integer xint = (Integer) holder2.get();
System.out.println(automobile);
System.out.println(str);
System.out.println(xint);
}
}

让我们先来看看结果

com.objects.springmvc.controllet.paradigm.Automobile@13d12d43
is 张三,NOT IS Automobile
1

只用了一个Holder2对象,却先后三次存储了三个不同类型的对象,Holder2可以存储任何类型的对象 -------------------------------------------------------------------------------------------------------- 通常情况下我们只会使用容器来存储一种类型的对象,与其使用Object,我们更喜欢暂时不指定类型,而是稍后在决定具体使用什么类型。为达到目的我们需要使用类型参数,用尖括号括住,放在类名的后面。使用这个类时在用实际的类型替换此类型参数。泛型的主要目的之一就是用来指定容器要持有什么类型的对象。

public class Holder3<T> {
public T aa;
public Holder3(T aa){ this.aa =aa; }
public T get(){ return aa; }
public void set(T aa){ this.aa = aa; }

public static void main(String[] args) {
Holder3<Automobile> holder3 = new Holder3<Automobile>(new Automobile());
Automobile automobile = holder3.get();
// holder3.set("aaa"); Error
// holder3.set(1);     Error
System.out.println(automobile);
}
}

你可以认为泛型与其他的类型差不多,只不过他们碰巧有类型参数罢了。在给大家看一个网上简单易懂的例子:

public class GenericTest {

public static void main(String[] args) {
List list = new ArrayList();
list.add("qqyumidi");
list.add("corn");
list.add(100);

for (int i = 0; i < list.size(); i++) {
String name = (String) list.get(i); // 1
System.out.println("name:" + name);
}
}
}
/**
* 定义了一个List类型的集合,先向其中加入了两个字符串类型的值,随后加入一个Integer类型的值。这是完全允许的,
* 因为此时list默认的类型为Object类型。在之后的循环中,由于忘记了之前在list中也加入了Integer类型的值或其他
* 编码原因,很容易出现类似于//1中的错误。因为编译阶段正常,而运行时会出
* 现“java.lang.ClassCastException”异常。因此,导致此类错误编码过程中不易发现。
*/


泛型接口

泛型也可以应用与接口,例如生成器是一种专门负责创建对象的类。实际上是工厂方法设计模式的一种应用。在科普一点小知识:生成器创建新对象是不需要传参就可以创建,而工厂工厂方法模式一版需要参数。

interface Holder4impl<T,X>{
void show(T t,X u);
}
public class Holder4 implements Holder4impl<String,Date>{
@Override
public void show(String str,Date date) {
System.out.println(str);
System.out.println(date);
}

public static void main(String[] args) {
Holder4 showTest=new Holder4();
showTest.show("Hello",new Date());
}
}

运行结果

Hello
Thu May 25 00:16:33 CST 2017


泛型方法

到目前为止,我们看到的泛型都是应用于整个类上。接下来我们来看看泛型方法的例子

public class Holder5 {
public static <T, U> T get(T t, U u) {
if (u != null)
return t;
else
return null;
}
public static void main(String[] args) {
String str=get("Hello", "World");
System.out.println(str);
}
}

java泛型中T、E、K、V、?等含义

因为时间太晚的缘故,今天到此为止明天继续。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息