-1

BankAccounts の配列リストに同等のインターフェイスを実装しようとしましたが、特に Collection.sort(list) 行についてテスター クラスのメイン メソッドをコンパイルして実行しようとすると、重大なエラーが発生しました。構文を認識しないと言っています..オンラインでjavadocを調べましたが、どこが間違っているのかわかりません..

public class BankAccount implements Comparable { //QUESTION 2.1
  /**
  A bank account has a balance that can be changed by 
  deposits and withdrawals.
 */
  private int accountNumber;
   private double balance; 
  /**
  Constructs a bank account with a zero balance
  @param anAccountNumber the account number for this account
  */
  public BankAccount(int anAccountNumber)
  {   
  accountNumber = anAccountNumber;
  balance = 0;
  }

   /**
  Constructs a bank account with a given balance
  @param anAccountNumber the account number for this account
  @param initialBalance the initial balance

*/ public BankAccount(int anAccountNumber, double initialBalance) {
accountNumber = anAccountNumber; バランス = 初期バランス; }

  /**
  Gets the account number of this bank account.
  @return the account number
  */
  public int getAccountNumber()
  {   
   return accountNumber;
   } 

  /**
  Deposits money into the bank account.
  @param amount the amount to deposit
  */
  public void deposit(double amount)
  {  
   double newBalance = balance + amount;
   balance = newBalance;
  }

  /**
  Withdraws money from the bank account.
  @param amount the amount to withdraw
  */
   public void withdraw(double amount)
   {   
     double newBalance = balance - amount;
     balance = newBalance;
   }

  /**
   Gets the current balance of the bank account.
   @return the current balance
  */
   public double getBalance()
   {   
     return balance;
   }


   public int compareTo (BankAccount temp) {


     if (balance<temp.balance) 
         return -1;
     if (balance==temp.balance) 
         return 0;
     return 1;
  }

}

 public class TestSortedBankAccounts {

    public static void main(String[] args) {
        // Put bank accounts into a list 
        ArrayList<BankAccount> list = new ArrayList<BankAccount>();

        BankAccount ba1 = new BankAccount(100, 500); //Constructor acctNumber and balance
        BankAccount ba2 = new BankAccount(200, 10000);
        BankAccount ba3 = new BankAccount(300, 400);
        BankAccount ba4 = new BankAccount(600, 0);
        BankAccount ba5 = new BankAccount(800, 50);

        list.add(ba1);
        list.add(ba2);
        list.add(ba3);
        list.add(ba4);
        list.add(ba5);

        // Call the library sort method
        Collections.sort(list);

        // Print out the sorted list 
        for (int i = 0; i < list.size(); i++) {
            BankAccount b = list.get(i);
            System.out.println(b.getBalance());
        }
    }
}

更新: TestSortedBankAccounts.java:26: エラー: sort(ArrayList) Collections.sort(list); に適したメソッドが見つかりません。^ メソッド Collections.sort(List,Comparator) は適用されません (実引数リストと仮引数リストの長さが異なるため、引数からインスタンス化できません) メソッド Collections.sort(List) は適用されません (推論された型は宣言されたバインドに準拠しません)推論: BankAccount 境界: Comparable) ここで、T#1、T#2 は型変数です: T#1 extends Object 宣言されたメソッド sort(List,Comparator) T#2 extends Comparable 宣言されたメソッド sort(List) 1エラー

4

2 に答える 2

4

の raw バージョンを実装していComparableます。の一般的な形式を実装する必要がありますComparable

public class BankAccount implements Comparable<BankAccount> {

生のフォームを実装する場合、パラメータ タイプは にcompareToなりますObject。ジェネリック形式を使用すると、ジェネリック型パラメーターcompareToを既に持っているようにパラメーターとして指定できます。

于 2013-10-29T00:15:01.393 に答える
0

私の答え(私が試したところ、うまくいきました):

public class BankAccount implements Comparable<Object> { // <-- 
  /**
  A bank account has a balance that can be changed by 
  deposits and withdrawals.
 */
  private int accountNumber;
   private double balance; 
  /**
  Constructs a bank account with a zero balance
  @param anAccountNumber the account number for this account
  */
  public BankAccount(int anAccountNumber)
  {   
  accountNumber = anAccountNumber;
  balance = 0;
  }

   /**
  Constructs a bank account with a given balance
  @param anAccountNumber the account number for this account
  @param initialBalance the initial balance
  */ 
  public BankAccount(int anAccountNumber, double initialBalance) {
      accountNumber = anAccountNumber; balance = initialBalance; }

  /**
  Gets the account number of this bank account.
  @return the account number
  */
  public int getAccountNumber()
  {   
   return accountNumber;
   } 

  /**
  Deposits money into the bank account.
  @param amount the amount to deposit
  */
  public void deposit(double amount)
  {  
   double newBalance = balance + amount;
   balance = newBalance;
  }

  /**
  Withdraws money from the bank account.
  @param amount the amount to withdraw
  */
   public void withdraw(double amount)
   {   
     double newBalance = balance - amount;
     balance = newBalance;
   }

  /**
   Gets the current balance of the bank account.
   @return the current balance
  */
   public double getBalance()
   {   
     return balance;
   }


   // public int compareTo (BankAccount temp) { // <-- WRONG
   public int compareTo(Object temp){ // <-- RIGHT

     BankAccount other = (BankAccount)temp; // <-- Cast to BankAccount
     if (balance<other.balance) 
         return -1;
     if (balance==other.balance) 
         return 0;
     return 1;
  }

}
于 2014-09-23T17:01:54.710 に答える