集册 Java实例教程 泛型方法最大地返回三个对象中最大的。

泛型方法最大地返回三个对象中最大的。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

472
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
泛型方法最大地返回三个对象中最大的。
/** from N o w J a v a . c o m - 时代Java**/

public class Main

{

   public static void main(String[] args) 

   {

      System.out.printf("Maximum of %d, %d and %d is %d%n%n", 3, 4, 5, 

         maximum(3, 4, 5));

      System.out.printf("Maximum of %.1f, %.1f and %.1f is %.1f%n%n", 

         6.6, 8.8, 7.7, maximum(6.6, 8.8, 7.7));

      System.out.printf("Maximum of %s, %s and %s is %s%n", "pear", 

         "apple", "orange", maximum("pear", "apple", "orange"));

   } 


   // determines the largest of three Comparable objects/*时 代 J a v a 公 众 号 提 供*/

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

   {

      T max = x; // assume x is initially the largest 


      if (y.compareTo(max) > 0)

         max = y; 
展开阅读全文