java - 关于接口(interface)的不适用方法错误

标签 java arrays interface compare string-comparison

我在 FindLargest 类中的一种方法 findLargest(Comparable[ ] arr) 遇到问题,该方法旨在返回数组中的最大元素。

显然,在 Lab5Main 类中,此方法适用于分数,但我收到日期编译错误。在系统打印输出命令中,我收到此错误:

The method findLargest(Comparable[ ]) in the type FindLargest is not applicable for the arguments (MyDate[ ])



这里是 假定程序输出 :
The largest fraction is 9/4
The latest date is 18/8/2011

我的代码如下所示:
public class Lab5Main {

  public static void main(String[] args) {

    // Fraction
    Fraction fractions[] = new Fraction[3];
    fractions[0] = new Fraction(1, 2);
    fractions[1] = new Fraction(6, 11);
    fractions[2] = new Fraction(9, 4);
    System.out.println("The largest fraction is " + FindLargest.findLargest(fractions));

    // MyDate
    MyDate dates[] = new MyDate[3];
    dates[0] = new MyDate(1898, 6, 9);
    dates[1] = new MyDate(2003, 4, 1);
    dates[2] = new MyDate(2011, 8, 18);
    System.out.println("The latest date is " + FindLargest.findLargest(dates));

  }
}
public class FindLargest {
    public static <T extends Comparable<? super T>> T findLargest(T[] arr) {
        if (arr.length == 0) {
            return null;
        }
        T max = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (arr[i].compareTo(max) > 0) {
                max = arr[i];
            }
        }
        return max;
    }
public class Fraction implements Comparable<Fraction> { 
    private int num, denom;

public int gcd(int a, int b)  {  
        if (a%b==0) {
            return b;
        }
        return gcd(b, a%b);  
    } 

    public void reduce()  {  
        int g = gcd(num, denom);  

        num = num / g;  
        denom = denom / g;  
    }  

    public Fraction(int n, int d) {

        if (n==0) {
            d=1;
        }
        if (d==0) {
            n=1;
            d=2;
        }
        if(d<0) {
            n=-n;
            d=-d;
        }

        num = n;
        denom = d;

        reduce();
    }

    public String toString() {
        return num + "/" + denom;
    }

@Override
      public int compareTo(Fraction f) {
//        Fraction f = (Fraction) obj;
          if (Math.abs(value() - f.value()) < 0.00001) {
              return 0;
          }
          if (value() > f.value()) {
              return 1;
          }
          return -1;
      }
      public int compareTo(Object obj) {
          Fraction f = (Fraction) obj;
          if (Math.abs(value() - f.value()) < 0.00001) {
              return 0;
          }
          if (value() > f.value()) {
              return 1;
          }
          return -1;
      }

}

public class MyDate implements Comparable<MyDate> {
  private int year, month, day;

public MyDate(int z, int y, int x) {
      if (z<1000 || z>3000) {
          z = 2000;
      }
      if (y<1 || y>12) {
          y = 1;
      }
      if (x<1 || x>31) {
          x = 1;
      }

      day = x;
      month = y;
      year = z;
  }

  public String toString() {
        return day + "/" + month + "/" + year;
    }

  @Override
  public int compareTo (MyDate date){
//    MyDate date = (MyDate) obj;
      int diffYear = year - date.year;
      if (diffYear < 0) {
          return -1;
      }
      else if (diffYear > 0) {
          return 1;
      }

      int diffMonth = month - date.month;
      if (diffMonth < 0) {
          return -1;
      }
      else if (diffMonth > 0) {
          return 1;
      }

      int diffDay = day - date.day;
      if (diffDay < 0) {
          return -1;
      }
      else if (diffDay > 0) {
          return 1;
      }

      return 0;
  }
}

最佳答案

问题是原始类型和泛型之一。 Comparable是一个通用接口(interface),但您使用原始类型 实现它和 您正在将它与原始类型一起使用。你应该解决这个问题。就像是,

public static <T extends Comparable<? super T>> T findLargest(T[] arr) {
    if (arr.length == 0) {
        return null;
    }
    T max = arr[0];
    for (int i = 0; i < arr.length; i++) {
        if (arr[i].compareTo(max) > 0) {
            max = arr[i];
        }
    }
    return max;
}

将确保您调用 findLargest使用泛型 T可以与它自己及其所有的父类(super class)进行比较。您还应该更改 FractionMyDate .就像是,
public class Fraction implements Comparable<Fraction> { 
    // ...
    @Override
    public int compareTo(Fraction f) {
        if (Math.abs(value() - f.value()) < 0.00001) {
            return 0;
        }
        if (value() > f.value()) {
            return 1;
        }
        return -1;
    }
}

MyDate相似地。我建议您始终使用 @Override当你打算重写一个方法时注释。

关于java - 关于接口(interface)的不适用方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61044080/

相关文章:

javascript - AngularJS 数组被重置

java - Java 中接口(interface)的结构约定是什么?

java - 单元测试默认方法调用接口(interface)中的抽象方法

Java 接口(interface) - 契约(Contract)中到底有什么?

Java Web Start (JWS) 内存管理在 32 位和 64 位中似乎有所不同

java - 是否有任何更新的 Linkedin java sdk?

java - 为单元测试自动注入(inject)依赖

java - 字符串数组长度显示为 1,即使调用逗号 (,) 分隔后数组为空

java - 从 Arrays.asList 转换为 Array

c++ - 创建结构数组