2012-03-01 44 views
0

我正在做一個類的任務,我們正在比較一個對象數組Customer,然後向用戶提供一些方法。我不斷收到一個錯誤,指出「客戶不是抽象的,並且不覆蓋java.lang.Comparable中的抽象方法compareTo(Customer)。如果已經在此論壇上討論過,我很抱歉,但是我找到一個答案時遇到了問題所有這些無意義的。可比較實現中的錯誤

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; 
    } 
} 
+0

你得到編譯錯誤? – lolo 2012-03-01 23:48:34

回答

0

按照Comparable<T>接口的文檔,你應該覆蓋的方法是

public int compareTo(T o) 

其中用於比較的第一個對象是this

您正在定義

public int compareTo(Customer a, Customer b) 

它不覆蓋正確的方法。這就是爲什麼Java編譯器抱怨你說Customer執行Comparable<Customer>的事實,但實際上你錯過了正確的方法。

後者是Comparator<Customer>所需的方法,該方法提供相同的功能,但採用「對象獨立」的方式。

1

你的類Customer工具Comparable<Customer>

class Customer implements Comparable<Customer> 

這就要求您實現以下方法

public int compareTo(Customer that) 

注意單個參數。它應該比較thisthat

0

當你實現一個接口時,你必須使用它提供的方法簽名。實現接口的要點是讓其他人知道你已經實現了他們需要的必要方法。在你的情況下,你有一個方法碰巧在界面中聲明瞭相同的名稱,但不是那些知道如何處理Comparable的類所期望的方法。

想象一下,您必須編寫一個在其他對象上調用compareTo方法的類。您希望通過單個參數(類型T),並且人們繼續使用各種參數類型(字符串,整數,可比較等)以及不同數量的參數實現它們自己的版本,例如compareTo(boolean b, String s, int minMatches)

compareTo(float f, boolean caseSensitive)

你怎麼知道哪一個打電話?