?? generalprogram.java
字號(hào):
package book.j2se5;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* 范型編程
*/
public class GeneralProgram {
/**
* 使用問(wèn)號(hào)?通配符,?代表任何類(lèi)型,所以它的參數(shù)可以是任何類(lèi)型的Collection。
*/
public static void printCollection(Collection<?> collect){
if (collect == null){
return;
}
for(Object obj : collect){
System.out.print(obj + " ");
}
System.out.println();
}
/**
* 使用有限制的通配符“? extends”,可以接受任何Parent及其子類(lèi)的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();
}
/**
* 范型方法,將一個(gè)任意類(lèi)型的數(shù)組,添加到列表中。
* @param <T> 代表一個(gè)任意的類(lèi)
* @param datas 數(shù)組對(duì)象
* @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對(duì)象中查找名字為name的Parent對(duì)象
* @param <T> 可以是Parent對(duì)象或者子類(lèi)對(duì)象
* @param parents Parent對(duì)象組
* @param name 目標(biāo)name
* @return 匹配的Parent對(duì)象
*/
public static <T extends Parent> T findParent(T[] parents, String name) {
if (parents == null) {
return null;
}
T parent = null;
// 依次遍歷Parent對(duì)象組
for (T x : parents) {
// 如果Parent對(duì)象的name與參數(shù)name匹配,則退出遍歷
if (x.name.equals(name)) {
parent = x;
break;
}
}
// 返回
return parent;
}
public static void main(String[] args) {
/** * 指定具體的類(lèi)型 ** */
// 聲明一個(gè)用于裝字符串的列表,該列表只能裝字符串類(lèi)型的對(duì)象
List<String> list_S = new ArrayList<String>();
list_S.add("first");
list_S.add("second");
list_S.add("third");
// 不能裝整數(shù)對(duì)象
Integer iObj = 10;
// list_S.add(iObj);// error!!!
// 在從列表中取值時(shí),不用作強(qiáng)制類(lèi)型轉(zhuǎn)換。
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!!!
/** * 通配符 *** */
// 調(diào)用具有“?”通配符的方法
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);
// 調(diào)用具有“? extends” 通配符的方法
// 只接受父類(lèi)以及子類(lèi)類(lèi)型的列表
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);
// 不能接受其他類(lèi)型的參數(shù)
// printNames(list_S);// error!!!
/** * 范型方法 *** */
// arrayToList方法將任意類(lèi)型的對(duì)象數(shù)組變成相應(yīng)的列表
Integer[] iObjs = { 55, 66, 77, 88, 99 };
// 轉(zhuǎn)換整型數(shù)組
List<Integer> result_I = arrayToList(iObjs);
printCollection(result_I);
String[] ss = { "temp", "temptemp", "hehe", "he", "hehehe" };
// 轉(zhuǎn)換字符串?dāng)?shù)組
List<String> result_S = arrayToList(ss);
printCollection(result_S);
// findParent方法在一組Parent對(duì)象中根據(jù)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),
new Child("def", 22) };
Child child = findParent(children, "bcd");
System.out.println("找到的bcd:" + child);
// 但是不能在字符串?dāng)?shù)組中進(jìn)行查找
// String sss = findParent(ss, "temp");// error!!!
}
}
// 父類(lèi)
class Parent{
public String name;
public Parent(String name){
this.name = name;
}
public String toString(){
return "name = " + this.name;
}
}
// 子類(lèi)
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;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -