亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長(zhǎng)資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    java如何定義受限制的類(lèi)型參數(shù)

    java如何定義受限制的類(lèi)型參數(shù)

    【相關(guān)學(xué)習(xí)推薦:java基礎(chǔ)教程】

    有時(shí)您可能想限制可以在參數(shù)化類(lèi)型中用作類(lèi)型參數(shù)的類(lèi)型。 例如,對(duì)數(shù)字進(jìn)行操作的方法可能只希望接受Number或其子類(lèi)的實(shí)例。 這就是有界類(lèi)型參數(shù)的用途。

    受限制參數(shù)類(lèi)型的方法示例

    要聲明有界類(lèi)型參數(shù),請(qǐng)列出類(lèi)型參數(shù)的名稱(chēng),后跟extends關(guān)鍵字,然后是其上限,在本例中為Number

    請(qǐng)注意,在這種情況下,extends通常用于表示“擴(kuò)展”(如在類(lèi)中)或“實(shí)現(xiàn)”(如在接口中)。

    package generics;  /**  * 定義受限制的方法  *   * @author psdxdgK1DT  *  */ public class Box<T> {  	private T t;  	public void set(T t) { 		this.t = t; 	}  	public T get() { 		return t; 	} /** 	 * 通過(guò)修改我們的通用泛型方法以包含此有界類(lèi)型參數(shù),現(xiàn)在編譯將失敗,因?yàn)槲覀儗?duì)inspect的調(diào)用仍包含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");這里會(huì)出現(xiàn)預(yù)編譯錯(cuò)誤  		integerBox.inspect(10); 	} }

    在顯示器上會(huì)出現(xiàn)紅色的波浪線表示編譯錯(cuò)誤

    java如何定義受限制的類(lèi)型參數(shù)

    如果強(qiáng)行編譯則會(huì)報(bào)錯(cuò):

    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)

    譯文:

    未解決的編譯錯(cuò)誤

    Box類(lèi)的inspect(U)方法不可應(yīng)用于(String)類(lèi)型參數(shù)

    使用受限類(lèi)型參的類(lèi)可調(diào)用受限邊界方法

    除了限制可用于實(shí)例化泛型類(lèi)型的類(lèi)型外,有界類(lèi)型參數(shù)還允許您調(diào)用在邊界中定義的方法:

    //使用受限類(lèi)型參數(shù)的類(lèi) 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方法通過(guò)n調(diào)用Integer類(lèi)中定義的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

    泛型算法

    有界類(lèi)型參數(shù)是實(shí)現(xiàn)泛型算法的關(guān)鍵。考慮下面的方法,該方法計(jì)算數(shù)組T[]中大于指定元素elem的元素?cái)?shù)。

    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)   //因?yàn)檫@里的T是受限制的類(lèi)型參數(shù),實(shí)現(xiàn)了Comparable接口,于是可以使用接口的方法compareTo     if (e.compareTo(elem) > 0)       ++count;   return count; }

    相關(guān)學(xué)習(xí)推薦:編程視頻

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)