日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 限制参数类型_java定义受限制的类型参数操作

發布時間:2023/12/13 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 限制参数类型_java定义受限制的类型参数操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

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

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

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

package generics;

/**

* 定義受限制的方法

*

* @author psdxdgK1DT

*

*/

public class Box{

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 * @param u

*/

public void inspect(U u) {

System.out.println("T:" + t.getClass().getName());

System.out.println("U:" + u.getClass().getName());

}

public static void main(String[] args) {

BoxintegerBox = new Box();

integerBox.set(new Integer("some text"));

integerBox.inspect("some test");這里會出現預編譯錯誤

integerBox.inspect(10);

}

}

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

如果強行編譯則會報錯:

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{

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:

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 { /* … */ } If bound A is not specified first, you get a compile-time error:

class D { /* … */ } // compile-time error

泛型算法

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

public static 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 Comparableinterface:

public interface Comparable{

public int compareTo(T o);

}

The resulting code will be:

public static > int countGreaterThan(T[] anArray, T elem) {

int count = 0;

for (T e : anArray)

//因為這里的T是受限制的類型參數,實現了Comparable接口,于是可以使用接口的方法compareTo

if (e.compareTo(elem) > 0)

++count;

return count;

}

以上這篇java定義受限制的類型參數操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

總結

以上是生活随笔為你收集整理的java 限制参数类型_java定义受限制的类型参数操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。