不适用的方法错误,涉及接口

huangapple go评论99阅读模式
英文:

Inapplicable method error regarding interfaces

问题

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("最大的分数是 " + 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("最晚的日期是 " + 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) {
		if (Math.abs(value() - f.value()) < 0.00001) {
			return 0;
		}
		if (value() > f.value()) {
			return 1;
		}
		return -1;
	}
	
	@Override
	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){
	  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;
  }
}
英文:

I'm having an issue with one of my methods findLargest(Comparable[ ] arr) in class FindLargest that's meant to return the largest element in an array.

Apparently, in class Lab5Main this method works fine with fractions, but I get a compilation error with dates. In the system print out command, I'm getting this error:

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

Here is the supposed program output:

The largest fraction is 9/4
The latest date is 18/8/2011

My codes are shown below:

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(&quot;The largest fraction is &quot; + 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(&quot;The latest date is &quot; + FindLargest.findLargest(dates));
}
}
public class FindLargest {
public static &lt;T extends Comparable&lt;? super T&gt;&gt; T findLargest(T[] arr) {
if (arr.length == 0) {
return null;
}
T max = arr[0];
for (int i = 0; i &lt; arr.length; i++) {
if (arr[i].compareTo(max) &gt; 0) {
max = arr[i];
}
}
return max;
}
public class Fraction implements Comparable&lt;Fraction&gt; {	
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&lt;0) {
n=-n;
d=-d;
}
num = n;
denom = d;
reduce();
}
public String toString() {
return num + &quot;/&quot; + denom;
}
@Override
public int compareTo(Fraction f) {
//		  Fraction f = (Fraction) obj;
if (Math.abs(value() - f.value()) &lt; 0.00001) {
return 0;
}
if (value() &gt; f.value()) {
return 1;
}
return -1;
}
public int compareTo(Object obj) {
Fraction f = (Fraction) obj;
if (Math.abs(value() - f.value()) &lt; 0.00001) {
return 0;
}
if (value() &gt; f.value()) {
return 1;
}
return -1;
}
}
public class MyDate implements Comparable&lt;MyDate&gt; {
private int year, month, day;
public MyDate(int z, int y, int x) {
if (z&lt;1000 || z&gt;3000) {
z = 2000;
}
if (y&lt;1 || y&gt;12) {
y = 1;
}
if (x&lt;1 || x&gt;31) {
x = 1;
}
day = x;
month = y;
year = z;
}
public String toString() {
return day + &quot;/&quot; + month + &quot;/&quot; + year;
}
@Override
public int compareTo (MyDate date){
//	  MyDate date = (MyDate) obj;
int diffYear = year - date.year;
if (diffYear &lt; 0) {
return -1;
}
else if (diffYear &gt; 0) {
return 1;
}
int diffMonth = month - date.month;
if (diffMonth &lt; 0) {
return -1;
}
else if (diffMonth &gt; 0) {
return 1;
}
int diffDay = day - date.day;
if (diffDay &lt; 0) {
return -1;
}
else if (diffDay &gt; 0) {
return 1;
}
return 0;
}
}

答案1

得分: 2

问题涉及原始类型和泛型。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;
}

将确保你使用一个泛型类型 T 来调用 findLargest,这个类型可以与自身及其所有的父类进行比较。你还应该修改 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 注解。

英文:

The issue is one of raw-types and generics. Comparable is a generic interface, but you are implementing it with a raw-type and you are using it with raw-types. You should fix that. Something like,

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

will ensure that you call findLargest with a generic type T that can be compared with itself and all of its' superclasses. You should also change Fraction and MyDate. Something like,

public class Fraction implements Comparable&lt;Fraction&gt; { 
// ...
@Override
public int compareTo(Fraction f) {
if (Math.abs(value() - f.value()) &lt; 0.00001) {
return 0;
}
if (value() &gt; f.value()) {
return 1;
}
return -1;
}
}

And MyDate similarly. I would recommend you always use the @Override annotation when you intend to override a method.

huangapple
  • 本文由 发表于 2020年4月5日 22:21:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/61044080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定