# Java泛型多重邊界

``````public static <T extends Number & Comparable<T>> T maximum(T x, T y, T z)
``````

• maximum - 最大值是一種通用方法。
• T - 通用型別引數傳遞給泛型方法。 它可以採取任何物件。

## 描述

T是傳遞給泛型類的型別引數應該是`Number`類的子型別，並且必須包含`Comparable`介面。 如果一個類作為系結傳遞，它應該在介面之前先傳遞，否則編譯時會發生錯誤。

## 範例

``````package com.yiibai;

public class MultipleBounds {
public static void main(String[] args) {
System.out.printf("Max of %d, %d and %d is %d\n\n", 30, 24, 15,
maximum(30, 24, 15));

System.out.printf("Max of %.1f,%.1f and %.1f is %.1f\n\n", 16.6, 28.8,
17.7, maximum(16.6, 28.8, 17.7));
}

public static <T extends Number & Comparable<T>> T maximum(T x, T y, T z) {
T max = x;
if (y.compareTo(max) > 0) {
max = y;
}

if (z.compareTo(max) > 0) {
max = z;
}
return max;
}

// Compiler throws error in case of below declaration
/*
* public static <T extends Comparable<T> & Number> T maximum1(T x, T y, T
* z) { T max = x; if(y.compareTo(max) > 0) { max = y; }
*
* if(z.compareTo(max) > 0) { max = z; } return max; }
*/
}
``````

``````Max of 30, 24 and 15 is 30

Max of 16.6,28.8 and 17.7 is 28.8
``````