java泛型進修示例。本站提示廣大學習愛好者:(java泛型進修示例)文章只能為提供參考,不一定能成為您想要的結果。以下是java泛型進修示例正文
Java泛型(Generics)是JDK5開端引入的一個新特征,許可在界說類和接口的時刻應用類型參數(Type Parameter)。聲明的類型參數在應用時器具體的類型來調換,如今泛型最重要的運用是在JDK5中的新聚集類框架中,Map, List均有效到。個中的長處不問可知,我們可以橫向擴大更多的類,缺陷呢,其實也就是他的長處,由於這須要我們在應用泛型類的時刻,要很清晰本身的代碼目地,不克不及應用毛病的類型。
最根本的泛型類
package com.garinzhang.javabase.generic.e1;
/**
* 最根本的泛型類,類型由本身界說
* @author Garin Zhang
*
* @param <T>
*/
public class Point<T> {
private T var;
public T getVar() {
return var;
}
public void setVar(T var) {
this.var = var;
}
}
package com.garinzhang.javabase.generic.e1;
public class GenericExample {
/**
* @param args
*/
public static void main(String[] args) {
Point<String> p = new Point<String> ();
p.setVar("coder");
System.out.println(p.getVar());
}
}
多個泛型類型
package com.garinzhang.javabase.generic.e2;
/**
* 多個泛型類型,普通多個最好是以接近T的字母,如S,R等
* @author Garin Zhang
*
* @param <T>
* @param <S>
*/
public class Notepad<T, S> {
private T key;
private S value;
public T getKey() {
return this.key;
}
public S getValue() {
return this.value;
}
public void setKey(T key) {
this.key = key;
}
public void setValue(S value) {
this.value = value;
}
}
package com.garinzhang.javabase.generic.e2;
public class GenericExample {
/**
* @param args
*/
public static void main(String[] args) {
Notepad<String, Integer> p = new Notepad<String, Integer> ();
p.setKey("coder");
p.setValue(99999);
System.out.println("key: " + p.getKey());
System.out.println("value: " + p.getValue());
}
}
在辦法參數中應用通配符"?"
package com.garinzhang.javabase.generic.e3;
/**
* 該例子症結在main辦法裡
* @author Garin Zhang
*
* @param <T>
*/
public class Info<T> {
private T key;
public T getKey() {
return this.key;
}
public void setKey(T key) {
this.key = key;
}
@Override
public String toString() {
return this.key.toString();
}
}
package com.garinzhang.javabase.generic.e3;
/**
* 在辦法參數中應用通配符
* @author Garin Zhang
*
*/
public class GenericExample {
/**
* @param args
*/
public static void main(String[] args) {
Info<String> i = new Info<String>();
i.setKey("coder");
fun(i);
Info<Integer> j = new Info<Integer>();
j.setKey(9999);
fun(j);
}
public static void fun(Info<?> temp) {
System.out.println("Content: " + temp);
}
}
向上轉型掉敗
package com.garinzhang.javabase.generic.e4;
/**
* 該例子症結在main辦法裡
* @author Garin Zhang
*
* @param <T>
*/
public class Info<T> {
private T key;
public T getKey() {
return this.key;
}
public void setKey(T key) {
this.key = key;
}
@Override
public String toString() {
return this.key.toString();
}
}
package com.garinzhang.javabase.generic.e4;
public class GenericExample {
/**
* @param args
*/
public static void main(String[] args) {
Info<String> strEg = new Info<String>();
Info<Object> objEg;
// 編譯毛病"Type mismatch: cannot convert from Info<String> to Info<Object>"
// 向上轉型掉敗,String -> Object
// objEg = strEg;
}
}
泛型在接口中的應用
package com.garinzhang.javabase.generic.e5;
/**
* 該例子症結在main辦法裡
* @author Garin Zhang
*
* @param <T>
*/
interface Info<T> {
public T getVar();
}
package com.garinzhang.javabase.generic.e5;
/**
* 泛型類
* @author Garin Zhang
*
* @param <T>
*/
public class InfoImpl<T> implements Info<T> {
private T var;
public InfoImpl(T var) {
this.setVar(var);
}
public void setVar(T var) {
this.var = var;
}
public T getVar() {
return this.var;
}
}
package com.garinzhang.javabase.generic.e5;
/**
* 非泛型類
* @author Garin Zhang
*
* @param <T>
*/
public class InfoImpl1 implements Info<String> {
private String var;
public InfoImpl1(String var) {
this.setVar(var);
}
public void setVar(String var) {
this.var = var;
}
public String getVar() {
return this.var;
}
}
package com.garinzhang.javabase.generic.e5;
public class GenericExample {
/**
* @param args
*/
public static void main(String[] args) {
Info<String> strEg = new InfoImpl<String>("coder");
System.out.println("Content: " + strEg.getVar());
Info<String> strEg1 = new InfoImpl1("coder1");
System.out.println("Content: " + strEg1.getVar());
}
}
通配符和extends, super的應用
package com.garinzhang.javabase.generic.e6;
/**
* 該例子症結在main辦法裡
* @author Garin Zhang
*
* @param <T>
*/
public class Info<T> {
private T key;
public T getKey() {
return this.key;
}
public void setKey(T key) {
this.key = key;
}
@Override
public String toString() {
return this.key.toString();
}
}
package com.garinzhang.javabase.generic.e6;
public class GenericExample {
/**
* @param args
*/
public static void main(String[] args) {
Info<String> strEg = new Info<String>();
strEg.setKey("coder");
// 編譯報錯"The method fun(Info<? extends Number>) in the type GenericExample is not applicable for the arguments (Info<String>)"
// upTypeLimit(i);
// 應用Integer,Number類型都可以
Info<Integer> intEg = new Info<Integer>();
intEg.setKey(9999);
upTypeLimit(intEg);
// 編譯報錯"The method downTypeLimit(Info<? super String>) in the type GenericExample is not applicable for the arguments (Info<Integer>)"
// downTypeLimit(intEg);
// 因為應用的是super,downTypeLimit只能吸收String自己和Object
// 檢查了String的繼續關系,沒有繼續其他類,只要Object
downTypeLimit(strEg);
Info<Object> objEg = new Info<Object>();
objEg.setKey(999);
downTypeLimit(objEg);
}
/**
* <? extends T> 表現類型的上界,表現參數化類型的能夠是T 或是 T的子類
* @param temp
*/
public static void upTypeLimit(Info<? extends Number> temp) {
System.out.println("Content: " + temp);
}
/**
* <? super T> 表現類型下界(Java Core中叫超類型限制),表現參數化類型是此類型的超類型(父類型),直至Object
* 在此例中,表現T只能為Object或String,由於String只繼續於Object
* @param temp
*/
public static void downTypeLimit(Info<? super String> temp) {
System.out.println("Content: " + temp);
}
}
辦法泛型,辦法外面多個泛型
package com.garinzhang.javabase.generic.e7;
/**
* 辦法泛型,辦法外面多個泛型
* @author Garin Zhang
*
* @param <T>
*/
public class Info {
/**
* 格局:辦法潤飾付 <以逗號離隔的類型列表>前往值類型辦法名(參數列表)
* 例如:public <T, S> T fun(T t, S s)
* @param t
* @param s
* @return
*/
public <T, S> T fun(T t, S s) {
System.out.println(s.toString());
return t;
}
}
package com.garinzhang.javabase.generic.e7;
public class GenericExample {
/**
* @param args
*/
public static void main(String[] args) {
Info info = new Info();
String str = info.fun("coder", "print second generic param");
System.out.println(str);
int i = info.fun(30, "print second param again");
System.out.println(i);
}
}
辦法中傳入或前往的泛型類型由挪用辦法時所設置的參數類型決議
package com.garinzhang.javabase.generic.e8;
/**
* extends
* @author Garin Zhang
*
* @param <T>
*/
public class Info<T extends Number> {
private T var;
public T getVar() {
return this.var;
}
public void setVar(T var) {
this.var = var;
}
@Override
public String toString() {
return this.var.toString();
}
}
package com.garinzhang.javabase.generic.e8;
public class GenericExample {
/**
* @param args
*/
public static void main(String[] args) {
Info<Integer> intEg = fun(30); // 這裡類型曾經肯定為Integer
System.out.println(intEg.getVar());
}
/**
* 辦法中傳入或前往的泛型類型由挪用辦法時所設置的參數類型決議
* @param param
* @return
*/
public static <T extends Number> Info<T> fun(T param) {
Info<T> temp = new Info<T>();
temp.setVar(param);
return temp;
}
}
讓辦法中傳入兩個參數類型堅持分歧
package com.garinzhang.javabase.generic.e9;
/**
* 檢查main
* @author Garin Zhang
*
* @param <T>
*/
public class Info<T> {
private T var;
public T getVar() {
return this.var;
}
public void setVar(T var) {
this.var = var;
}
@Override
public String toString() {
return this.var.toString();
}
}
package com.garinzhang.javabase.generic.e9;
public class GenericExample {
/**
* @param args
*/
public static void main(String[] args) {
Info<String> i1 = new Info<String>();
i1.setVar("Hello");
Info<String> i2 = new Info<String>();
i2.setVar("Coder");
Info<Integer> i3 = new Info<Integer>();
i3.setVar(999);
add(i1, i2);
//編譯毛病"The method add(Info<T>, Info<T>) in the type GenericExample is not applicable for the arguments (Info<String>, Info<Integer>)"
// add(i1, i3);
}
/**
* 辦法中傳入兩個參數類型必需分歧
* @param param
* @return
*/
public static <T> void add(Info<T> i1, Info<T> i2) {
System.out.println(i1.getVar() + ":" + i2.getVar());
}
}
泛型,可變參數,相似於javascript裡的Arguments對象
package com.garinzhang.javabase.generic.e10;
public class GenericExample {
/**
* @param args
*/
public static void main(String[] args) {
Integer i[] = fun(1, 2, 3, 4, 5, 6);
fun2(i);
}
public static <T> T[] fun(T... arg) {
return arg;
}
public static <T> void fun2(T param[]) {
System.out.println("generic array: ");
for(T t : param) {
System.out.println(t + " ,");
}
}
}
泛型嵌套:應用泛型類做為參數;依據前往值類型肯定前往值
package com.garinzhang.javabase.generic.e11;
/**
* 接收兩個泛型類型
* @author Garin Zhang
*
* @param <T>
*/
public class Info<T, V> {
private T var;
private V value;
public T getVar() {
return this.var;
}
public void setVar(T var) {
this.var = var;
}
public V getValue(){
return this.value;
}
public void setValue(V value) {
this.value = value;
}
@Override
public String toString() {
return this.var.toString();
}
}
package com.garinzhang.javabase.generic.e11;
/**
* 接收1個泛型類型
* @author Garin Zhang
*
* @param <T>
*/
public class Demo<S> {
private S info;
public Demo(S info) {
this.setInfo(info);
}
public void setInfo(S info) {
this.info = info;
}
public S getInfo() {
return this.info;
}
}
package com.garinzhang.javabase.generic.e11;
import java.util.List;
import com.谷歌.common.collect.Lists;
public class GenericExample {
/**
* @param args
*/
public static void main(String[] args) {
Demo<Info<String, Integer>> d;
Info<String, Integer> i;
i = new Info<String, Integer>();
i.setVar("Coder");
i.setValue(999);
d = new Demo<Info<String,Integer>>(i);
System.out.println("Content: " + d.getInfo().getVar());
System.out.println("Content: " + d.getInfo().getValue());
System.out.println(query(1, 2, 3, 4, 5).toString()); // [1, 2, 3, 4, 5]
// 正告"Type safety: A generic array of Object&Comparable<?>&Serializable is created for a varargs parameter"
System.out.println(query(1, 2, 3, "StringType").toString()); // [1, 2, 3, StringType]
System.out.println(query("I ", "am ", "a ", "coder").toString());// [I , am , a , coder]
List<String> list = Lists.newArrayList("I ", "am ", "a ", "coder");
System.out.println(list.toString()); // [I , am , a , coder]
}
/**
* 經由過程前往值肯定泛型類型,這個辦法外面的前往值類型,是由辦法的界說主動生成的
* @param elements
* @return
*/
public static <E> List<E> query(E... elements) {
// https://github.com/exitsoft/exit-web-framework/commit/1d2f1098a2a4b6abab175b793e2308aa8bd0ea16.
// import com.谷歌.common.collect.Lists;
// <dependency>
// <groupId>com.谷歌.guava</groupId>
// <artifactId>guava</artifactId>
// <version>16.0.1</version>
// </dependency>
return Lists.newArrayList(elements);
}
}