gpt4 book ai didi

java - 实现Comparable接口(interface)的问题

转载 作者:行者123 更新时间:2023-12-02 00:56:27 25 4
gpt4 key购买 nike

我有以下代码,是我在《Java for Everyday》一书中实现的第 9 章中实现的:

package Inheritance;

import java.util.Scanner;

interface Comparable{
int compareTo(Object otherObject);
}

interface Measurable{
double getMeasure();
}


public class BankAccount implements Comparable, Measurable {
private double balance;
private int counter;
public String account;

public BankAccount(){
balance = 0;
counter = 0;
account = "";
}

public void SetAccount(String accountNumber, double amount){
account = accountNumber;
balance = amount;
}

public void Deposit(double amount){
balance = balance + amount;
counter = counter + 1;
System.out.println("Account: "+ account + " has balance: " + balance);
}

public void Withdraw(double amount){
balance = balance - amount;
counter = counter + 1;
System.out.println("Account: "+ account + " has balance: " + balance);
}

public double getBalance(){
return balance;
}

public void MonthEnd(){
counter = 0;
}

public void printMenu(){
System.out.println("Please choose your options to proceed: ");
System.out.println("D)eposit W)ithdraw M)onth end Q)uit");
}

public double getMeasure(){
return balance;
}

public int compareTo(Object otherObject) {
BankAccount other = (BankAccount) otherObject;
if (balance < other.balance) {
return -1;
}
if (balance > other.balance) {
return 1;
}
return 0;
}

然后我按如下方式运行它:

public class Implementation {

public static void main(String[] args) {

//Implement Comparable Interface example
BankAccount[] accounts2 = new BankAccount[3];
accounts2[0] = new BankAccount();
accounts2[0].SetAccount("SA001", 10000);
accounts2[1] = new BankAccount();
accounts2[1].SetAccount("SA001", 5000);
accounts2[2] = new BankAccount();
accounts2[2].SetAccount("SA001", 20000);
System.out.println(accounts2[0].compareTo(accounts2[1]));

Arrays.sort(accounts2);
}
}

但是,它在我的 Comparable 界面上给了我以下错误。我已经在 BankAccount 类中提供了compareTo 方法,为什么它不起作用?

线程“main”中的异常 java.lang.ClassCastException:类 Inheritance.BankAccount 无法转换为类 java.lang.Comparable(Inheritance.BankAccount 位于加载程序“app”的未命名模块中; java.lang.Comparable 位于加载程序“bootstrap”的 java.base 模块中) 在 java.base/java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:320)

at java.base/java.util.ComparableTimSort.sort(ComparableTimSort.java:188)

at java.base/java.util.Arrays.sort(Arrays.java:1250)

at Inheritance.Implementation.main(Implementation.jav

最佳答案

摆脱:

interface Comparable{
int compareTo(Object otherObject);
}

因为您通过创建自己的接口(interface)来掩盖同名的核心 Java 接口(interface),从而使编译器感到困惑。。相反,请使用核心 Java Comparable 接口(interface)。

这里的问题是 Arrays.sort(...) 方法必须使用核心接口(interface),而不是您自己的接口(interface)。否则,您的代码可能可能会工作,尽管可能会让其他编码人员感到困惑,更重要的是您 future 的自己

旁注,我建议使用该界面的通用版本。我还喜欢总是在我认为可能覆盖的任何方法上方添加@Override注释,以便编译器对其进行测试我并在编译时警告我:

public class BankAccount implements Comparable<BankAccount>, Measurable {

@Override
public int compareTo(BankAccount other) {
// ....
}

关于java - 实现Comparable接口(interface)的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61305821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com