欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

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

請注意,在這種情況下,extends通常用于表示“擴展”(如在類中)或“實現(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;
	}
/**
	 * 通過修改我們的通用泛型方法以包含此有界類型參數(shù),現(xiàn)在編譯將失敗,因為我們對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");這里會出現(xiàn)預編譯錯誤

		integerBox.inspect(10);
	}
}

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

如果強行編譯則會報錯:

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)類型參數(shù)\

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

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

//使用受限類型參數(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方法通過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

泛型算法

有界類型參數(shù)是實現(xiàn)泛型算法的關鍵??紤]下面的方法,該方法計算數(shù)組T[]中大于指定元素elem的元素數(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)
  //因為這里的T是受限制的類型參數(shù),實現(xiàn)了Comparable接口,于是可以使用接口的方法compareTo
    if (e.compareTo(elem) > 0)
      ++count;
  return count;
}

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

相關文章

  • Spring Aop基本流程原理示例詳解

    Spring Aop基本流程原理示例詳解

    這篇文章主要給大家介紹了關于Spring Aop基本流程原理的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • Java MD5加密工具類的方法(支持多參數(shù)輸入)

    Java MD5加密工具類的方法(支持多參數(shù)輸入)

    在實際開發(fā)過程中,MD5加密是一種常見的數(shù)據(jù)安全處理手段,常用于密碼存儲、數(shù)據(jù)完整性校驗等場景,這篇文章主要介紹了Java MD5加密工具類(支持多參數(shù)輸入),需要的朋友可以參考下
    2024-05-05
  • SWT(JFace)體驗之StackLayout布局

    SWT(JFace)體驗之StackLayout布局

    SWT(JFace)體驗之StackLayout布局實現(xiàn)代碼。
    2009-06-06
  • Java實現(xiàn)動態(tài)獲取文件的絕對路徑

    Java實現(xiàn)動態(tài)獲取文件的絕對路徑

    我們知道在?Java?中讀取一些配置文件信息,是在開發(fā)中十分常用的要求。這篇文章就來和大家聊聊Java如何實現(xiàn)動態(tài)獲取文件的絕對路徑,感興趣的可以了解一下
    2023-02-02
  • Java使用GUI實現(xiàn)貪吃蛇游戲詳解

    Java使用GUI實現(xiàn)貪吃蛇游戲詳解

    小時候經(jīng)常在諾基亞上玩的一個小游戲-貪吃蛇,你還記得嗎?本篇帶你重溫一下把它實現(xiàn),做的比較簡單,但還是可以玩的.感興趣的朋友快來看看吧
    2022-05-05
  • Java的JDBC中Statement與CallableStatement對象實例

    Java的JDBC中Statement與CallableStatement對象實例

    這篇文章主要介紹了Java的JDBC中Statement與CallableStatement對象實例,JDBC是Java編程中用于操作數(shù)據(jù)庫的API,需要的朋友可以參考下
    2015-12-12
  • redis中存儲list<map>,list<entity>的處理

    redis中存儲list<map>,list<entity>的處理

    本文主要介紹了redis中存儲list<map>,list<entity>的處理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-06-06
  • 老生常談 java匿名內部類

    老生常談 java匿名內部類

    下面小編就為大家?guī)硪黄仙U刯ava匿名內部類。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-07-07
  • java中String的一些方法深入解析

    java中String的一些方法深入解析

    以下是對java中String的一些方法進行了詳細的分析介紹,需要的朋友可以參考下
    2013-07-07
  • Java兩種動態(tài)代理JDK動態(tài)代理和CGLIB動態(tài)代理詳解

    Java兩種動態(tài)代理JDK動態(tài)代理和CGLIB動態(tài)代理詳解

    這篇文章主要介紹了Java兩種動態(tài)代理JDK動態(tài)代理和CGLIB動態(tài)代理詳解,代理模式是23種設計模式的一種,他是指一個對象A通過持有另一個對象B,可以具有B同樣的行為的模式,為了對外開放協(xié)議,B往往實現(xiàn)了一個接口,A也會去實現(xiàn)接口,需要的朋友可以參考下
    2023-11-11

最新評論