[英]Can't figure out a compilation error for simple Java app
This is the error I am getting: Error: The type Customer must implement the inherited abstract method java.lang.Comparable.compareTo(Customer) 这是我得到的错误: 错误:类型Customer必须实现继承的抽象方法java.lang.Comparable.compareTo(Customer)
I'm comparing it to some lab work that I did and it looks exactly the same, yet that compiled just fine. 我正在将其与我所做的一些实验室工作进行比较,它看起来完全一样,但编译起来还不错。 I'm not sure what's going on here. 我不确定这里发生了什么。
Here is the code segment in question which, incidentally, was written by my professor: 这是有问题的代码段,偶然地,这是我的教授编写的:
class Customer implements Comparable<Customer>
{
String name;
double purchase;
double rebate;
public Customer(String n, double p, double r)
{ name=n;
purchase=p;
rebate=r;
} //end Constructor
public Customer(String n)
{
name=n;
} //end Constructor
public String toString()
{
return String.format("%-20s%10.2f%10.2f", name, purchase, rebate);
}
/*
Here, define the method comparedTo of the Comparable
interface so that an array of Customer objects can
be sorted by customer name
*/
public int comparedTo(Customer a)
{
return this.name.compareTo(a.name);
} //end comparedTo
} //end class Customer
Oh, and here are the inclusions the professor included: 哦,这是教授包括的物品:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
Your help is greatly appreciated! 非常感谢您的帮助!
comparedTo
should be compareTo
. comparedTo
应compareTo
。
The error says it all. 错误说明了一切。 ;p ,p
As Steven correctly pointed out, the name of the method you were attempting to implement was misspelled. 正如史蒂文正确指出的那样,您尝试实现的方法名称拼写错误。 A great way to catch errors like that is the @Override
annotation, like this: 捕获此类错误的好方法是@Override
注释,如下所示:
@Override
public int comparedTo(Customer a)
This tells the compiler that you intended comparedTo() to override a method declared in a parent class. 这告诉编译器,您打算让compareTo()覆盖在父类中声明的方法。 When the compiler doesn't find a comparedTo() method to override, it will tell you. 当编译器找不到要重写的compareTo()方法时,它将告诉您。 As of Java 1.6, @Override
can also be used on methods declared in interfaces. 从Java 1.6开始, @Override
也可以在接口中声明的方法上使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.