008 The Generics In JAVA
泛型是JAVA的核心特型之一,我们先看一个例子:
没有使用泛型前,如下:
import java.util.ArrayList;
import java.util.List; public class GenericsStu {
public static void main(String[] args) { List list = new ArrayList();
String name = "gavin";
Integer age = 5;
list.add(name);
list.add(age); for (Object obj : list) {
String str = (String) obj; // Exception in thread "main"
// java.lang.ClassCastException:
// java.lang.Integer cannot be cast to
// java.lang.String
}
}
}
使用泛型后如下:
import java.util.ArrayList;
import java.util.List; public class GenericsStu {
public static void main(String[] args) { List<String> list = new ArrayList<String>();
String name = "gavin";
Integer age = 5;
list.add(name);
list.add("hello");
list.add(age);//这个在编译时就提示类型不匹配,如果用的是eclipse,会及时显示错误,避免了运行时异常。 for (String str : list) {
System.out.println(str); }
}
}
泛型也可以用于Class,例如:
使用泛型前,是这样的:
public class GenericsStu { private Object t; public Object get() {
return t;
} public void set(Object t) {
this.t = t;
} public static void main(String args[]){
GenericsStu type = new GenericsStu();
type.set(2);
String str = (String) type.get(); //Exception in thread "main" java.lang.ClassCastException:
//java.lang.Integer cannot be cast to java.lang.String
System.out.println(str);
}
}
使用泛型后,是这样的:
public class GenericsStu<T> { private T t; public T get(){
return this.t;
} public void set(T t){
this.t=t;
} public void print(){
System.out.println(t);
} public static void main(String args[]){
GenericsStu<String> type = new GenericsStu<String>();
type.set("gavin");
type.print();//Output is:gavin
type.set(2); //编译就不通过,避免了运行时java.lang.ClassCastException
type.print(); }
}
008 The Generics In JAVA的更多相关文章
- thinking in java Generics Latent typing
The beginning of this chapter introduced the idea of writing code that can be applied as generally a ...
- Java ConcurrentHashMap Example and Iterator--转
原文地址:http://www.journaldev.com/122/java-concurrenthashmap-example-iterator#comment-27448 Today we wi ...
- Thinking in Java——笔记(15)
Generics The term "generic" means "pertaining or appropriate to large groups of class ...
- Java深度历险(五)——Java泛型
作者 成富 发布于 2011年3月3日 | 注意:QCon全球软件开发大会(北京)2016年4月21-23日,了解更多详情!17 讨论 分享到:微博微信FacebookTwitter有道云笔记邮件 ...
- 在Android中使用Java 8的lambda表达式
作为一名Java开发者,或许你时常因为缺乏闭包而产生许多的困扰.幸运的是:Java's 8th version introduced lambda functions给我们带来了好消息;然而,这咩有什 ...
- Effective Java Index
Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...
- [CareerCup] 14.4 Templates Java模板
14.4 Explain the difference between templates in C++ and generics in Java. 在Java中,泛式编程Generic Progra ...
- java基础之 泛型
泛型(Generic type 或者generics)是对 Java 语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类.可以把类型参数看作是使用参数化类型时指定的类型的一个占位符,就像方法 ...
- Java之泛型练习
package cn.itcast.generics; import java.util.Comparator; import java.util.Iterator; import java.util ...
随机推荐
- API文档中,<E>、<T>、<?>分别代表什么意思
although they are all type parameters, the convention is:E ElementT or S TypeK Key, as in <K, V&g ...
- MS CRM 2011的自定义和开发(11)——插件(plugin)开发(一)
http://www.cnblogs.com/StoneGarden/archive/2012/02/02/2336147.html MS CRM 2011的自定义和开发(11)——插件(plugin ...
- android studio添加三方jar包
jar包放项目的libs目录,然后tools,android,sync project with grade files即可.
- python使用xlrd模块读写Excel文件的方法
本文实例讲述了python使用xlrd模块读写Excel文件的方法.分享给大家供大家参考.具体如下: 一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi ...
- 【转】CSS浏览器兼容性与解析问题终极归纳
1.怪异模式问题:漏写DTD声明,Firefox仍然会按照标准模式来解析网页,但在IE中会触发怪异模式.为避免怪异模式给我们带来不必要的麻烦,最好养成书写DTD声明的好习惯. 2.IE6双边距问题:在 ...
- duplicate symbols
duplicate symbol _mCollecatView in: /Users/Rubert/Library/Developer/Xcode/DerivedData/ChengDuHidengD ...
- Form_Form树形结构HTree的开发(案例)
2014-06-09 Created By BaoXinjian
- PLSQL_基础系列01_正则表达REGEXP_LIKE / SUBSTR / INSTR / REPLACE(案例)
2014-11-30 Created By BaoXinjian
- mvc无法找到资源
昨天装了vs2015,但是第二步没有完成.今天急急忙忙的用13打开一个mvc的项目,但是添加的控制器怎么都不能访问. 无法找到资源. 说明: HTTP 404.您正在查找的资源(或者它的一个依赖项)可 ...
- 带节日和农历的js日历 带农历的脚本:
<html><head><meta http-equiv="Content-Type" content="text/html; charse ...