最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java定義受限制的類型參數(shù)操作

 更新時(shí)間:2020年08月22日 10:29:09   作者:占東紅  
這篇文章主要介紹了java定義受限制的類型參數(shù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

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

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

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

請(qǐng)注意,在這種情況下,extends通常用于表示“擴(kuò)展”(如在類中)或“實(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ò)修改我們的通用泛型方法以包含此有界類型參數(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ò)誤

如果強(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類的inspect(U)方法不可應(yīng)用于(String)類型參數(shù)\

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

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

//使用受限類型參數(shù)的類
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類中定義的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

泛型算法

有界類型參數(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是受限制的類型參數(shù),實(shí)現(xiàn)了Comparable接口,于是可以使用接口的方法compareTo
    if (e.compareTo(elem) > 0)
      ++count;
  return count;
}

以上這篇java定義受限制的類型參數(shù)操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring Cloud Config解決的問(wèn)題和案例

    Spring Cloud Config解決的問(wèn)題和案例

    Spring Cloud Config 是 Spring Cloud 套件中的一個(gè)工具,提供了在分布式系統(tǒng)中對(duì)外部化配置的服務(wù)器端和客戶端支持,本文介紹了Spring Cloud Config解決的問(wèn)題和案例,文中有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下
    2024-07-07
  • java實(shí)現(xiàn)超市商品庫(kù)存管理平臺(tái)

    java實(shí)現(xiàn)超市商品庫(kù)存管理平臺(tái)

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)超市商品庫(kù)存管理平臺(tái),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • Spring Bean的實(shí)例化之屬性注入源碼剖析過(guò)程

    Spring Bean的實(shí)例化之屬性注入源碼剖析過(guò)程

    本篇文章主要就是分析Spring源碼剖析-Bean的實(shí)例化-屬性注入的相關(guān)知識(shí),通過(guò)本文學(xué)習(xí)AbstractAutowireCapableBeanFactory#populateBean 方法的主要功能就是屬性填充,感興趣的朋友跟隨小編一起看看吧
    2021-06-06
  • PC 端微信掃碼注冊(cè)和登錄實(shí)例

    PC 端微信掃碼注冊(cè)和登錄實(shí)例

    這篇文章主要介紹了PC 端微信掃碼注冊(cè)和登錄實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • Java 遍歷list和map的方法

    Java 遍歷list和map的方法

    這篇文章主要介紹了Java 遍歷list和map的方法,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-12-12
  • 解讀JDK1.8?默認(rèn)使用什么垃圾收集器

    解讀JDK1.8?默認(rèn)使用什么垃圾收集器

    這篇文章主要介紹了解讀JDK1.8?默認(rèn)使用什么垃圾收集器,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Spring?IOC容器的Bean管理基于注解屬性注入方式

    Spring?IOC容器的Bean管理基于注解屬性注入方式

    這篇文章主要為大家介紹了Spring?IOC容器的Bean管理基于注解屬性注入方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Java靜態(tài)static與實(shí)例instance方法示例

    Java靜態(tài)static與實(shí)例instance方法示例

    這篇文章主要為大家介紹了Java靜態(tài)static與實(shí)例instance方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Mybatis?Plus?中的LambdaQueryWrapper示例詳解

    Mybatis?Plus?中的LambdaQueryWrapper示例詳解

    這篇文章主要介紹了Mybatis?Plus?中的LambdaQueryWrapper,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • Spring Boot的Controller控制層和頁(yè)面

    Spring Boot的Controller控制層和頁(yè)面

    這篇文章主要介紹了Spring Boot的Controller控制層和頁(yè)面,需要的朋友可以參考下
    2017-04-04

最新評(píng)論

噶尔县| 黔西县| 汕头市| 弥渡县| 贡山| 祥云县| 海门市| 循化| 柳林县| 囊谦县| 凤冈县| 建平县| 原平市| 涿鹿县| 望城县| 且末县| 凌海市| 中山市| 万山特区| 无为县| 肇东市| 五华县| 弥勒县| 玛纳斯县| 缙云县| 肥东县| 朝阳县| 曲阳县| 阿坝| 洪洞县| 柯坪县| 皋兰县| 兴仁县| 佳木斯市| 东方市| 巴马| 宜川县| 赞皇县| 新安县| 尼木县| 巨野县|