惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
NISL@THU
NISL@THU
E
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
K
Kaspersky official blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
S
Schneier on Security
G
GRAHAM CLULEY
The Hacker News
The Hacker News
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
爱范儿
爱范儿
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
S
Securelist
G
Google Developers Blog
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
美团技术团队
F
Fortinet All Blogs
小众软件
小众软件
Recorded Future
Recorded Future
V
Visual Studio Blog
B
Blog RSS Feed
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
Latest news
Latest news
Spread Privacy
Spread Privacy
H
Heimdal Security Blog

博客园 - fengye515

开发常用工具 Qt的内存管理 pyqt4制作透明无边框窗体 面向对象 Java 1.5新特性Enum的用法 Java线程学习和总结 JVM,反射与动态代理 JDK5新特性之二----新的格式化输出 JDK5新特性之一----静态导入 第五章Java虚拟机(Chapter Five: The Java Virtual Machine) Java虚拟机体系结构概述 Java虚拟机 面向对象的思维方法 JDK5.0的11个主要新特征 精通java的一些必要条件 Spring 入门 Hibernate快速入门 设计模式 用J2ME开发企业级无线应用
简单的范型实例程序
fengye515 · 2007-02-11 · via 博客园 - fengye515
import java.util.ArrayList;
            import java.util.Collection;
            import java.util.List;
/** * 范型编程 */
// 父类 class Parent{ public String name; public Parent(String name){ this.name = name; } public String toString(){ return "name = " + this.name; } }
// 子类
            class Child extends Parent{
            public int age;
            public Child(String name, int age){
            super(name);
            this.age = age;
            }
            public String toString(){
            return super.toString() + ";  age = " + age;
            }
            }
public class GeneralProgram { /** * 在命令行输出一个集 * 使用问号?通配符,?代表任何类型,所以它的参数可以是任何类型的Collection。 */ public static void printCollection(Collection< ?> collect){ if (collect == null){ return; } for(Object obj : collect){ System.out.print(obj + " "); } System.out.println(); }
/** * 使用有限制的通配符“? extends”,可以接受任何Parent及其子类的Collection * @param collect */ public static void printNames(Collection< ? extends Parent> collect){ if (collect == null){ return; } for(Parent parent : collect){ System.out.print(parent.name + " "); } System.out.println(); }
/** * 范型方法,将一个任意类型的数组,添加到列表中。 * @param < T> 代表一个任意的类 * @param datas 数组对象 * @return */
public static < T> List< T> arrayToList(T[] datas){ if (datas == null){ return null; } List< T> list_T = new ArrayList< T>(); for (T x : datas){ list_T.add(x); } return list_T; }
/** * 范型方法,在一组Parent对象中查找名字为name的Parent对象 * @param < T> 可以是Parent对象或者子类对象 * @param parents Parent对象组 * @param name 目标name * @return 匹配的Parent对象 */
public static < T extends Parent> T findParent(T[] parents, String name) { if (parents == null) { return null; } T parent = null; // 依次遍历Parent对象组 for (T x : parents) { // 如果Parent对象的name与参数name匹配,则退出遍历 if (x.name.equals(name)) { parent = x; break; } } // 返回 return parent; } public static void main(String[] args) { /** * 指定具体的类型 ** */ // 声明一个用于装字符串的列表,该列表只能装字符串类型的对象 List< String> list_S = new ArrayList< String>(); list_S.add("first"); list_S.add("second"); list_S.add("third");
// 不能装整数对象 Integer iObj = 10; // list_S.add(iObj);// error!!! // 在从列表中取值时,不用作强制类型转换。 String firstS = list_S.get(0); String thirdS = list_S.get(2); System.out.println("firstS: " + firstS + "; thirdS: " + thirdS);
/** **范型和继承** */ // String 继承 Object String s = "sss"; Object o = s; // 但List< String>不继承List< Object> // List< Object> list_O = list_S;// error!!!
/** * 通配符 *** */ // 调用具有“?”通配符的方法 List< Integer> list_I = new ArrayList< Integer>(); list_I.add(5555); list_I.add(6666); list_I.add(7777); // 该方法既可以打印整型列表,也可以打印字符串列表 printCollection(list_I); printCollection(list_S);
// 调用具有“? extends” 通配符的方法 // 只接受父类以及子类类型的列表 List< Parent> list_Parent = new ArrayList< Parent>(); list_Parent.add(new Parent("parentOne")); list_Parent.add(new Parent("parentTow")); list_Parent.add(new Parent("parentThree")); List< Child> list_Child = new ArrayList< Child>(); list_Child.add(new Child("ChildOne", 20)); list_Child.add(new Child("ChildTow", 22)); list_Child.add(new Child("ChildThree", 21)); printNames(list_Parent); printNames(list_Child);
// 不能接受其他类型的参数 // printNames(list_S);// error!!! /** * 范型方法 *** */
// arrayToList方法将任意类型的对象数组变成相应的列表 Integer[] iObjs = { 55, 66, 77, 88, 99 }; // 转换整型数组 List< Integer> result_I = arrayToList(iObjs); printCollection(result_I);
String[] ss = { "temp", "temptemp", "hehe", "he", "hehehe" }; // 转换字符串数组 List< String> result_S = arrayToList(ss); printCollection(result_S); // findParent方法在一组Parent对象中根据name查找Parent Parent[] parents = { new Parent("abc"), new Parent("bcd"),new Parent("def") };
Parent parent = findParent(parents, "bcd"); System.out.println("找到的bcd:" + parent); Child[] children = { new Child("abc", 22), new Child("bcd", 23),ew Child("def", 22) }; Child child = findParent(children, "bcd"); System.out.println("找到的bcd:" + child); // 但是不能在字符串数组中进行查找 // String sss = findParent(ss, "temp");// error!!! } } 运行结果: C:\java>java GeneralProgram
firstS: first; thirdS: third
5555 6666 7777
first second third
parentOne parentTow parentThree
ChildOne ChildTow ChildThree
55 66 77 88 99
temp temptemp hehe he hehehe
找到的bcd:name = bcd
找到的bcd:name = bcd; age = 23