JDK5.0新特性系列---8.泛型编程

简介:

import java.util.ArrayList;

import java.util.Collection;

import java.util.List;

/**

*泛型编程关键掌握两点:

*1.在方法参数中使用通配符

*2.在方法的参数类型和返回类型中使用通用类型

*/

/**关键技术

*1.通配符问号(?)表示任意类型."List<?>"表示可以存放任意对象类型的List

*2.通配符可以接extendssuper,表示有限制的通配符."List<? extends Parent>"

* 声明的List能且仅能存放Parent及其子类的对象,"List<? super Child>"

* 声明的List仅能存放Child及其父类的对象

*3.通用类型是指不指定参数或者返回值的类型,常用一个大写的字母代表类型,

* 它能代表任何类型,需要在方法声明的返回值前用<>声明通用类型."public <T> String

* getName(T data)"的方法声明中,String前用<T>表示T是通用类型,它的data参数的类型是T,

* 表示能接收任意类型的参数,方法的返回值是String类型

*/

public class GeneralProgram {

/**使用问号"?*通配符,"?"代表任何类型,所以它的参数可以是任何类型的Collection*/

public static void printCollection(Collection<?> collect){

if(collect == null){

return;

}

for(Object obj : collect){

System.out.println(obj + " ");

}

System.out.println();

}

/**使用有限制的通配符"? extends",可以接受任何Parent及其子类的Collection*/

public static void printNames(Collection<? extends Parent> collect){

if(collect == null){

return;

}

for(Object obj : collect){

System.out.println(obj + " ");

}

System.out.println();

}

/**将一个任意类型的数组添加到列表里*/

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对象中查找名字为nameParent对象*/

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);

/***调用具有"?"通配符的方法***/

//只接受父类以及子类类型的列表

List<Parent> list_Parent = new ArrayList<Parent>();

list_Parent.add(new Parent("parentOne"));

list_Parent.add(new Parent("parentTwo"));

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("childTwo",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"),newParent("def")};

Parent parent = findParent(parents, "bcd");

System.out.print("找到的bcd: "+parent);

Child[] children = {new Child("abc",22),new Child("bcd",23),newChild("def",22)};

Child child = findParent(children, "bcd");

System.out.print("找到的bcd: "+child);

//但是不能在字符串数组中进行查找

//String sss = findparent(ss,"temp");//error!

}

}

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;

}

}




本文转自远哥博客园博客,原文链接:http://www.cnblogs.com/taven/archive/2011/12/17/2291462.html,如需转载请自行联系原作者

相关文章
|
5天前
|
Java API
JDK8到JDK25版本升级的新特性问题之使用Collectors.teeing()来计算一个列表中学生的平均分和总分如何操作
JDK8到JDK25版本升级的新特性问题之使用Collectors.teeing()来计算一个列表中学生的平均分和总分如何操作
|
5天前
|
Java API Apache
JDK8到JDK24版本升级的新特性问题之在Java中,HttpURLConnection有什么局限性,如何解决
JDK8到JDK24版本升级的新特性问题之在Java中,HttpURLConnection有什么局限性,如何解决
|
5天前
|
Oracle Java 关系型数据库
JDK8到JDK29版本升级的新特性问题之未来JDK的升级是否会成为必然趋势,如何理解
JDK8到JDK29版本升级的新特性问题之未来JDK的升级是否会成为必然趋势,如何理解
|
5天前
|
Oracle 安全 Java
JDK8到JDK28版本升级的新特性问题之在Java 15及以后的版本中,密封类和密封接口是怎么工作的
JDK8到JDK28版本升级的新特性问题之在Java 15及以后的版本中,密封类和密封接口是怎么工作的
|
5天前
|
算法 Java iOS开发
JDK8到JDK27版本升级的新特性问题之JDK 17中G1在资源占用方面有何变化
JDK8到JDK27版本升级的新特性问题之JDK 17中G1在资源占用方面有何变化
|
5天前
|
XML JSON Java
JDK8到JDK26版本升级的新特性问题之在JDK 13中,字符串文本块改进字符串嵌入是如何实现的
JDK8到JDK26版本升级的新特性问题之在JDK 13中,字符串文本块改进字符串嵌入是如何实现的
|
5天前
|
Java 编译器 开发者
JDK8到JDK23版本升级的新特性问题之编写一个简单的module-info.java文件,如何实现
JDK8到JDK23版本升级的新特性问题之编写一个简单的module-info.java文件,如何实现
|
5天前
|
缓存 Oracle Java
JDK8到JDK22版本升级的新特性问题之在JDK17中,日志的刷新如何操作
JDK8到JDK22版本升级的新特性问题之在JDK17中,日志的刷新如何操作
|
5天前
|
安全 Java 编译器
JDK8到JDK21版本升级的新特性问题之JDK17重要的新特性有哪些
JDK8到JDK21版本升级的新特性问题之JDK17重要的新特性有哪些
|
5天前
|
算法
JDK8到JDK20版本升级的新特性问题之JDK14改进了NullPointerExceptions的提示信息,怎么实现的
JDK8到JDK20版本升级的新特性问题之JDK14改进了NullPointerExceptions的提示信息,怎么实现的