Java 泛型

  • 泛型

  • 對象的定義

    類名稱<具體類>  對象名稱 = new   類名稱<具體類> ();

class Point<T>{ T可以是任意的字母

private T value;    //封裝

public T getValue(){ //同類型

return value;

}

public void setValue(T value){

this.value=value;

}

}

 

class ListSetMap {

public static void main(String[] args) {

Point<String> a=new Point<String>(); //String可以改成其他類型

a.setValue("一些人");

System.out.println(a.getValue());

}

}

注意:已經指定的泛型是不能夠將其變量的類型改爲其他類型;

  • 泛型也有構造方法
  • 可以設置多個泛型類型
class Point<T,K>{

private T value1;

private K value2;

public Point(T value1,K value2){

this.value1=value1;

this.value2=value2;

}

public T getValue1(){

return value1;

}

public K getValue2(){

return value2;

}

}

 

class ListSetMap {

public static void main(String[] args) {

Point<Integer,String> a=new Point<Integer,String>(1,"嘿嘿");

System.out.println(a.getValue1()+"\n"+a.getValue2());

}

}

輸出        1

                嘿嘿

 

  • 通配符

在接收任意的對象時,不會對象是什麼類型,就可以用Point<?>  t來接收Point的對象

class Point<T>{

private T value1;

public Point(T value1){

this.value1=value1;

}

public T getValue1(){

return value1;

}

}

 

class ListSetMap {

public static void main(String[] args) {

Point<String> a= new Point<String>("a");

fun(a);

}

public static void fun(Point<?> t){

System.out.println(t.getValue1());

}

}
  • 受限泛型
class ListSetMap {

public static void main(String[] args) {

Point<String> a=new Point<String>("a");

//fun(a);     //不能被接收

fun2(a);

Point<Integer>b =new Point<Integer>(1);

fun(b);

//fun2(b); //不能被接收

}

public static void fun(Point<? extends Number> t){

System.out.println(t.getValue1());

}

public static void fun2(Point<? super String> t){

System.out.println(t.getValue1());

}

}

輸出            a

                    1


  • 泛型不能向上轉型
class ListSetMap {

public static void main(String[] args) {

Point<String> a=new Point<String>("a");

Point<Object> b=null;

b=a;

}

public static void fun(Point<? extends Number> t){

System.out.println(t.getValue1());

}

}

輸出ListSetMap.java:15: 錯誤: 不兼容的類型: Point<String>無法轉換爲Point<Object>

  • 通過泛型方法返回泛型類實例
class ListSetMap {

public static void main(String[] args) {

Point<Integer> a=fun(10); //fun(10)會返回一個實例

System.out.println(a.getValue1());

}

public static <T extends Number> Point<T> fun(T t){

Point<T> temp=new Point<T>(); //實例化

temp.setValue1(t);    

return temp; //返回實例-(對象)

}

}

輸出 10


 

  • 泛型數組
class ListSetMap {

public static void main(String[] args) {

Integer i[]=fun1(1,2,3,4,5);

fun2(i);

}

public static <T>T[] fun1(T...arg){ //接收可變參數

return arg;   //返回類型<T> T[]的

}

public static <T> void fun2(T p[]){

for(T t : p)

System.out.println(t);

}

}

  • 嵌套泛型類
class Point<T>{

private T value1;

public Point(T value1){

this.value1=value1;

}

public T getValue1(){

return value1;

}

public void setValue1(T value1){

this.value1=value1;

}

}

class Demo<S>{

private S info;

public Demo(S s){

this.info=s;

}

public void setInfo(S info){

this.info=info;

}

public S getInfo(){

return info;

}

}

class ListSetMap {

public static void main(String[] args) {

Demo<Point<String>> a=null; //將Point作爲Demo的泛型類型

Point<String> b=new Point<String>("嵌入類"); //實例化

a=new Demo<Point<String>>(b);

System.out.println(a.getInfo().getValue1()); //調用info

info=Point<String

}

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章