可比实现中的错误问题

时间:2012-03-01 23:45:21

标签: java

我正在为类进行分配,我们正在比较一个对象数组Customer,然后为用户提供了许多方法。我一直收到一条错误,上面写着“客户不是抽象的,并且没有覆盖java.lang.Comparable中的抽象方法compareTo(客户)。如果在这个论坛上处理了这个问题,我很抱歉,但是我找不到答案使所有这一切都变得敏感。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;


class prog4 {
public static void main(String args[]) 
            throws FileNotFoundException
{
Scanner kb=new Scanner(System.in);

Customer [] A =readArray(); //read data into objects in A

while(true)
{
  System.out.println();
  System.out.println();
  System.out.println("Please select one of the follwing actions: ");
  System.out.println("q - Quit"); 
  System.out.println("a - List the customer records sorted by customer name"); 
  System.out.println("b - Enter a customer name to find the customer's record"); 
  System.out.println("c - List the customer records sorted by purchase in descending order"); 
  System.out.println("Please enter q, a, b, or c: ");

  String selection=kb.nextLine();  //read user's selection
  if (selection.equals("")) continue; //break; //if selection is "", show menu again

  switch (selection.charAt(0))
  {
    /*write code to process the following cases*/

    case 'a':

            break;

    case 'b':

            break;

    case 'c':

            break;

    case 'q':

            return;

    default:
  } //end switch
} //end while
 } //end main();


  //the following method uses the data from indata.txt
  //to create Customer objects of an array
  //and returns the array

  private static Customer[] readArray()
                  throws FileNotFoundException
  {
    String name;
    double purchase;
    double rebate;

    Scanner infile=new Scanner(new File("indata.txt")); //input file

    int size=infile.nextInt();  //get number of lines
    infile.nextLine();          //skips end of line

    Customer A[]=new Customer[size];

    for (int k=0; k<size; k++)
    {
        //read a name = 16 characters
        infile.useDelimiter("");
        name="";

        for (int i=0; i<16; i++) name=name+infile.next();
        infile.reset();

        purchase=infile.nextDouble();
        rebate=infile.nextDouble();
        if (infile.hasNextLine()) infile.nextLine(); //skip end of line

        A[k]=new Customer(name, purchase, rebate); //create object for A[i]

     } //end for loop

    infile.close();
    return A;

  } //end readArray

  //the method prints the Customer objects of the array A
  //one customer record per line
  private static void printArray(Customer [] A)
  {
   for (Customer x:A)
    {System.out.println(x);}
  } //end printArray
}  //end class prog4


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);
  }

public int compareTo(Customer a, Customer b) 
 {return b.name.compareTo(a.name);}
  }
//end class Customer

class descendingPurchase implements Comparator<Customer>
{
 public int compare(Customer a, Customer b)
   {
     if(a.purchase<b.purchase) return 1;
     else if(a.purchase==b.purchase) return 0;
     else return -1;
   }
}

3 个答案:

答案 0 :(得分:1)

您的班级Customer实施Comparable<Customer>

class Customer implements Comparable<Customer>

要求您实现以下方法

public int compareTo(Customer that)

请注意单个参数。它应该将thisthat进行比较。

答案 1 :(得分:0)

根据Comparable<T>接口的文档,您应该覆盖的方法是

 public int compareTo(T o)

其中第一个比较对象是this

您正在定义

 public int compareTo(Customer a, Customer b) 

不会覆盖正确的方法。这就是为什么Java编译器抱怨你说Customer实现Comparable<Customer>但实际上你错过了正确的方法这一事实。

后者是Comparator<Customer>所需的方法,它提供相同的功能,但是以“对象独立”的方式。

答案 2 :(得分:0)

当您实现一个接口时,必须使用它提供的方法签名。实现接口的关键是让其他人知道您已经实现了他们需要的必要方法。在您的情况下,您的方法恰好在接口中声明了相同的名称,但不是那些知道如何处理Comparable的类所期望的方法。

想象一下,你必须编写一个在其他对象上调用compareTo方法的类。你希望传递一个param(类型为T)并且人们继续实现他们自己的版本,包括各种param类型(字符串,整数,可比较等)和不同数量的param类型,例如compareTo(boolean b, String s, int minMatches)

compareTo(float f, boolean caseSensitive)

你怎么知道打电话给哪一个?