Java 关于接口的不适用方法错误
我的findLargest类中的findLargest(Comparable[]arr)方法之一存在问题,该方法用于返回数组中最大的元素 显然,在Lab5Main类中,这个方法可以很好地处理分数,但我得到了一个日期编译错误。在系统打印输出命令中,我得到以下错误: findLargest类型中的方法findLargest(Compariable[])不适用于参数(MyDate[]) 以下是假定的程序输出:Java 关于接口的不适用方法错误,java,arrays,interface,compare,string-comparison,Java,Arrays,Interface,Compare,String Comparison,我的findLargest类中的findLargest(Comparable[]arr)方法之一存在问题,该方法用于返回数组中最大的元素 显然,在Lab5Main类中,这个方法可以很好地处理分数,但我得到了一个日期编译错误。在系统打印输出命令中,我得到以下错误: findLargest类型中的方法findLargest(Compariable[])不适用于参数(MyDate[]) 以下是假定的程序输出: The largest fraction is 9/4 The latest date is
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));
}
}
公共类FindLargest{
public static问题在于原始类型和泛型。Comparable
是一个泛型接口,但是您正在使用原始类型实现它,并且您正在使用原始类型。您应该解决这个问题。例如
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> {
// ...
@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
注释。谢谢您的回复。我做了修改,但仍然出现相同的错误:findLargest类型中的方法findLargest(T[])不适用于参数(MyDate[])将您的问题编辑为。我们无法运行您的代码。我必须修复一些问题,甚至要编译您发布的代码。例如,Fraction.value()
-您的构造函数和toString
不在这里。因此没有人可以尝试它。我做了我建议的更改,它在这里编译。您是否确实更改了MyDate
?您当前的代码没有实现Comparable
,您忘记了@覆盖注释。因为公共int compareTo(Object obj){
应该是public int comparieto(MyDate){
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;
}
}
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> {
// ...
@Override
public int compareTo(Fraction f) {
if (Math.abs(value() - f.value()) < 0.00001) {
return 0;
}
if (value() > f.value()) {
return 1;
}
return -1;
}
}