溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java中generic有什么用

發布時間:2021-07-15 11:16:45 來源:億速云 閱讀:166 作者:小新 欄目:編程語言

這篇文章將為大家詳細講解有關java中generic有什么用,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

一介紹:

在JavaSE1.5之前,沒有泛型的情況的下,通過對類型Object的引用來實現參數的“任意化”,“任意化”帶來的缺點是要做顯式的強制類型轉換,而這種轉換是要求開發者對實際參數類型可以預知的情況下進行的。對于強制類型轉換錯誤的情況,編譯器可能不提示錯誤,在運行的時候才出現異常,這是一個安全隱患。

泛型的好處是在編譯的時候檢查類型安全,并且所有的強制轉換都是自動和隱式的,提高代碼的重用率。

二、泛型參數:

class Gen<T> {
	private T ob;
	//定義泛型成員變量
	public Gen(T ob) {
		this.ob = ob;
	}
	public T getOb() {
		return ob;
	}
	public void setOb(T ob) {
		this.ob = ob;
	}
	public void showType() {
		System.out.println("T的實際類型是: " + ob.getClass().getName());
	}
}
public class GenericParameter {
	public static void main(String[] args){
		//定義泛型類Gen的一個Integer版本
		Gen<Integer> intOb=new Gen<Integer>(100);
		intOb.showType();
		int i= intOb.getOb();
		System.out.println("value= " + i);
		System.out.println("----------------------------------");
		//定義泛型類Gen的一個String版本
		Gen<String> strOb=new Gen<String>("Hello Dylan!");
		strOb.showType();
		String s=strOb.getOb();
		System.out.println("value= " + s);
	}
}

output:

T的實際類型是: java.lang.Integer
value= 100
----------------------------------
T的實際類型是: java.lang.String
value= Hello Dylan!

三、泛型類:

class GenericsFoo<T> {
	private T x;
	public GenericsFoo(T x) {
		this.x = x;
	}
	public T getX() {
		return x;
	}
	public void setX(T x) {
		this.x = x;
	}
}
public class GenericClass {
	public static void main(String args[]){
		GenericsFoo<String> strFoo=new GenericsFoo<String>("Hello Generics!");
		GenericsFoo<double> douFoo=new GenericsFoo<double>(new double("33"));
		GenericsFoo<Object> objFoo=new GenericsFoo<Object>(new Object());
		System.out.println("strFoo.getX="+strFoo.getX());
		System.out.println("douFoo.getX="+douFoo.getX());
		System.out.println("objFoo.getX="+objFoo.getX());
	}
}

output:

strFoo.getX=Hello Generics!
douFoo.getX=33.0
objFoo.getX=java.lang.Object@1d0fafc

四 限制泛型:

import java.util.ArrayList;
import java.util.Collection;
class CollectionGenFoo<T extends Collection> {
	private T x;
	public CollectionGenFoo(T x) {
		this.x = x;
	}
	public T getX() {
		return x;
	}
	public void setX(T x) {
		this.x = x;
	}
}
public class GenericRestrict {
	public static void main(String[] args) {
		CollectionGenFoo<ArrayList> listFoo = null;
		listFoo = new CollectionGenFoo<ArrayList>(new ArrayList());
		CollectionGenFoo<? extends Collection> listFoo1 = null;
		listFoo1=new CollectionGenFoo<ArrayList>(new ArrayList());
		System.out.println("實例化成功!");
	}
}

output:
實例化成功!

五 泛型方法:

public class GenericFunction {
	public <T> void f(T x) {
		System.out.println(x.getClass().getName());
	}
	public static void main(String[] args) {
		GenericFunction ea = new GenericFunction();
		ea.f(" ");
		ea.f(10);
		ea.f('a');
		ea.f(ea);
	}
}

output:
java.lang.String
java.lang.Integer
java.lang.Character
GenericFunction

-----------------------------
dylan presents.

關于“java中generic有什么用”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女