我有一个实现NamedAccount.java的BankAccount.java,它是一个抽象接口,但它一直给我一个错误,因为BankAccount.java不是抽象的,它不能覆盖它。我该如何解决这个问题?我尝试在两种情况下添加@Override但它不起作用!
BankAccount.java:
public class BankAccount implements NamedAccount {
private String myCustomer;
private double myBalance;
private double myInterest;
protected int myMonthlyWithdrawCount;
protected double myMonthlyServiceCharges;
public BankAccount(final String theNameOfOwner,
final double theInterestRate) {
myCustomer = theNameOfOwner;
myInterest = theInterestRate;
myBalance = 0.0;
myMonthlyWithdrawCount = 0;
myMonthlyServiceCharges = 0;
}
public double getBalance() {
return myBalance;
}
public boolean processDeposit(final double theAmount) {
boolean trueDeposit = false;
if (theAmount > 0) {
myBalance += theAmount;
trueDeposit = true;
}
return trueDeposit;
}
public boolean processWithdrawal(final double theAmount) {
boolean trueWithdrawal = false;
if (theAmount > 0 && theAmount > myBalance) {
myBalance -= theAmount;
trueWithdrawal = true;
}
return trueWithdrawal;
}
public double calculateInterest() {
return myBalance * (myInterest / 12.0);
}
public void performMonthlyProcess() {
myBalance -= myMonthlyServiceCharges;
myBalance += calculateInterest();
myMonthlyWithdrawCount = 0;
myMonthlyServiceCharges = 0.0;
if (myBalance < 0.0) {
myBalance = 0.0;
}
}
}
NamedAccount.java:
public interface NamedAccount {
String getAccountHolderName();
void setAccountHolderName(final String theNewName);
}
要让它运行,你需要这个SafeDepositBoxAccount.java:
public class SafeDepositBoxAccount implements NamedAccount {
private String mySafeName;
public SafeDepositBoxAccount(final String theNameOfHolder) {
mySafeName = theNameOfHolder;
}
public String getAccountHolderName() {
return mySafeName;
}
public void setAccountHolderName(final String theNewName) {
mySafeName = theNewName;
}
}
答案 0 :(得分:2)
错误告诉您必须覆盖这些方法(来自您的界面),因为该类不是abstract
。如果你使类抽象,那么你就不需要覆盖这些方法。像,
private String accountHolderName;
@Override
public String getAccountHolderName() {
return accountHolderName;
}
@Override
public void setAccountHolderName(final String theNewName) {
this.accountHolderName = theNewName;
}
答案 1 :(得分:1)
由于您的班级BankAccount
表示它实施NamedAccount
,您需要实施合同中定义的menthod here,即setAccountHolderName
和getAccountHolderName
在您的BankAccount类中,或者作为编译器,您需要将您的类定义为抽象类,因此其他类可以扩展您的抽象类并定义这两个方法以形成具体的类。
如果您想摆脱编译错误,那么您需要覆盖这两个方法,就像您在SafeDepositBoxAccount类中定义的那样:
public class BankAccount implements NamedAccount {
.....
private String accountHolderName;
@Override
public String getAccountHolderName() {
return accountHolderName;
}
@Override
public void setAccountHolderName(final String theNewName) {
this.accountHolderName = theNewName;
}
}
答案 2 :(得分:1)
在你的代码中,NamedAccount是一个接口,任何实现接口的类(在这种情况下为 BankAccount 和 SafeDepositBoxAccount )必须实现所有接口的方法,除非class被声明为 abstract 。
对于 BankAccount 类,既没有提供getAccountHolderName和setAccountHolderName的实现,也没有将NamedAccount类标记为 abstract 。因此发生了此错误。
有两种解决方案:
注意:当方法声明旨在覆盖超类中的方法声明时,使用@Override。 BankAccount中的任何方法在超类中都没有相应的方法,所以你不能@Override。
答案 3 :(得分:-2)
public abstract class BankAccount implements NamedAccount {
abstract String getAccountHolderName();
abstract void setAccountHolderName(final String theNewName);
}