站長資訊網
        最全最豐富的資訊網站

        java如何定義受限制的類型參數

        java如何定義受限制的類型參數

        【相關學習推薦:java基礎教程】

        有時您可能想限制可以在參數化類型中用作類型參數的類型。 例如,對數字進行操作的方法可能只希望接受Number或其子類的實例。 這就是有界類型參數的用途。

        受限制參數類型的方法示例

        要聲明有界類型參數,請列出類型參數的名稱,后跟extends關鍵字,然后是其上限,在本例中為Number

        請注意,在這種情況下,extends通常用于表示“擴展”(如在類中)或“實現”(如在接口中)。

        package generics;  /**  * 定義受限制的方法  *   * @author psdxdgK1DT  *  */ public class Box<T> {  	private T t;  	public void set(T t) { 		this.t = t; 	}  	public T get() { 		return t; 	} /** 	 * 通過修改我們的通用泛型方法以包含此有界類型參數,現在編譯將失敗,因為我們對inspect的調用仍包含String: 	 * By modifying our generic method to include this bounded type parameter 	 * compilation will now fail, since our invocation of inspect still includes a String: 	 * inspect:單詞:檢查 	 * @param <U> 	 * @param u 	 */ 	public <U extends Number> void inspect(U u) { 		System.out.println("T:" + t.getClass().getName()); 		System.out.println("U:" + u.getClass().getName()); 	}  	public static void main(String[] args) { 		Box<Integer> integerBox = new Box<Integer>(); 		integerBox.set(new Integer("some text")); 		integerBox.inspect("some test");這里會出現預編譯錯誤  		integerBox.inspect(10); 	} }

        在顯示器上會出現紅色的波浪線表示編譯錯誤

        java如何定義受限制的類型參數

        如果強行編譯則會報錯:

        program run result:

        Exception in thread “main” java.lang.Error: Unresolved compilation problem: The method inspect(U) in the type Box is not applicable for the arguments (String)

        at generics.Box.main(Box.java:36)

        譯文:

        未解決的編譯錯誤

        Box類的inspect(U)方法不可應用于(String)類型參數

        使用受限類型參的類可調用受限邊界方法

        除了限制可用于實例化泛型類型的類型外,有界類型參數還允許您調用在邊界中定義的方法:

        //使用受限類型參數的類 public class NaturalNumber<T extends Integer> {    private T n;   public NaturalNumber(T n) { this.n = n; }    public boolean isEven() {     return n.intValue() % 2 == 0;   }    // ...

        isEven方法通過n調用Integer類中定義的intValue方法。

        多重受限邊界(Multiple Bounds)

        The preceding example illustrates the use of a type parameter with a single bound, but a type parameter can have multiple bounds:

        <T extends B1 & B2 & B3> A type variable with multiple bounds is a subtype of all the types listed in the bound. If one of the bounds is a class, it must be specified first. For example:

        Class A { /* … / } interface B { / … / } interface C { / … */ }

        class D <T extends A & B & C> { /* … */ } If bound A is not specified first, you get a compile-time error:

        class D <T extends B & A & C> { /* … */ } // compile-time error

        泛型算法

        有界類型參數是實現泛型算法的關鍵。考慮下面的方法,該方法計算數組T[]中大于指定元素elem的元素數。

        public static <T> int countGreaterThan(T[] anArray, T elem) {   int count = 0;   for (T e : anArray)     if (e > elem) // compiler error       ++count;   return count; } The implementation of the method is straightforward, but it does not compile because the greater than operator (>) applies only to primitive types such as short, int, double, long, float, byte, and char.  You cannot use the > operator to compare objects. To fix the problem, use a type parameter bounded by the Comparable<T> interface:  public interface Comparable<T> {   public int compareTo(T o); } The resulting code will be:  public static <T extends Comparable<T>> int countGreaterThan(T[] anArray, T elem) {   int count = 0;   for (T e : anArray)   //因為這里的T是受限制的類型參數,實現了Comparable接口,于是可以使用接口的方法compareTo     if (e.compareTo(elem) > 0)       ++count;   return count; }

        相關學習推薦:編程視頻

        贊(0)
        分享到: 更多 (0)
        網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
        主站蜘蛛池模板: 久久久久久亚洲精品无码| 一区二区三区精品高清视频免费在线播放| 国产A√精品区二区三区四区| 最新国产乱人伦偷精品免费网站| 92精品国产自产在线观看| 久久精品国产清高在天天线| 日本午夜精品理论片A级APP发布| 国产成人精品综合在线观看| 国产成人精品综合网站| 国产精品亚洲一区二区三区在线 | 久久久久免费精品国产| 人妻少妇精品中文字幕AV| 牛牛在线精品观看免费正| 国产成人亚洲精品影院| 国产香蕉精品视频在| 国产成人精品一区二区秒拍 | 久草欧美精品在线观看| 97精品国产高清自在线看超 | 国产精品美女久久久| 992tv精品视频tv在线观看| 久久香蕉超碰97国产精品| 永久免费精品影视网站| 日韩精品一区二区三区在线观看 | 99国产欧美久久久精品蜜芽| 精品日韩亚洲AV无码| 亚洲精品蜜桃久久久久久| 偷拍精品视频一区二区三区| 久久久久久亚洲精品无码| 麻豆国产高清精品国在线| 久久国产精品免费一区| 国产精品综合专区中文字幕免费播放 | 国产精品国产三级国产潘金莲| 午夜精品在线观看| 亚洲精品欧美日韩| 国产精品毛片无码| 精品人妻少妇一区二区三区在线| 精品水蜜桃久久久久久久| 欧美在线精品永久免费播放| 欧美精品一区二区三区免费观看| 日韩人妻无码精品无码中文字幕| 亚洲精品视频免费观看|