Java 覆盖抽象接口方法
Java Override Abstract Interface Method
我有一个实现 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;
}
}
由于您的 class BankAccount
说它实现了 NamedAccount
,您需要同时实现合同中定义的方法,如 here 所述,即 setAccountHolderName
和 getAccountHolderName
在你的 BankAccount class 或者编译器说你需要将你的 class 定义为抽象 class,所以其他 class 可以扩展你的抽象 class 定义这两个方法,形成一个具体的class。
如果你想摆脱编译错误,那么你需要重写这两个方法,就像你在 SafeDepositBoxAccount 中定义的那样 class:
public class BankAccount implements NamedAccount {
.....
private String accountHolderName;
@Override
public String getAccountHolderName() {
return accountHolderName;
}
@Override
public void setAccountHolderName(final String theNewName) {
this.accountHolderName = theNewName;
}
}
错误告诉您您 必须 覆盖这些方法(从您的界面),因为 class 不是 abstract
。如果您将 class 抽象化,那么您将不需要重写这些方法。像,
private String accountHolderName;
@Override
public String getAccountHolderName() {
return accountHolderName;
}
@Override
public void setAccountHolderName(final String theNewName) {
this.accountHolderName = theNewName;
}
public abstract class BankAccount implements NamedAccount {
abstract String getAccountHolderName();
abstract void setAccountHolderName(final String theNewName);
}
在您的代码中,NamedAccount 是一个接口,任何实现该接口的 class(BankAccount 和 SafeDepositBoxAccount case) 必须实现接口的所有方法,除非 class 声明为 abstract。
对于 BankAccount class,既没有提供 getAccountHolderName 和 setAccountHolderName 的实现,也没有将 NamedAccount class 标记为 摘要。因此出现了这个错误。
有两种解决方案:
- 要么将 BankAccount class 声明为抽象的,
- 或者为 getAccountHolderName 和
setAccountHolderName 方法。
注意:@Override 在方法声明旨在覆盖 superclass 中的方法声明时使用。 BankAccount 中的任何方法在 superclass 中都没有相应的方法,所以你不能 @Override.
我有一个实现 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;
}
}
由于您的 class BankAccount
说它实现了 NamedAccount
,您需要同时实现合同中定义的方法,如 here 所述,即 setAccountHolderName
和 getAccountHolderName
在你的 BankAccount class 或者编译器说你需要将你的 class 定义为抽象 class,所以其他 class 可以扩展你的抽象 class 定义这两个方法,形成一个具体的class。
如果你想摆脱编译错误,那么你需要重写这两个方法,就像你在 SafeDepositBoxAccount 中定义的那样 class:
public class BankAccount implements NamedAccount {
.....
private String accountHolderName;
@Override
public String getAccountHolderName() {
return accountHolderName;
}
@Override
public void setAccountHolderName(final String theNewName) {
this.accountHolderName = theNewName;
}
}
错误告诉您您 必须 覆盖这些方法(从您的界面),因为 class 不是 abstract
。如果您将 class 抽象化,那么您将不需要重写这些方法。像,
private String accountHolderName;
@Override
public String getAccountHolderName() {
return accountHolderName;
}
@Override
public void setAccountHolderName(final String theNewName) {
this.accountHolderName = theNewName;
}
public abstract class BankAccount implements NamedAccount {
abstract String getAccountHolderName();
abstract void setAccountHolderName(final String theNewName);
}
在您的代码中,NamedAccount 是一个接口,任何实现该接口的 class(BankAccount 和 SafeDepositBoxAccount case) 必须实现接口的所有方法,除非 class 声明为 abstract。
对于 BankAccount class,既没有提供 getAccountHolderName 和 setAccountHolderName 的实现,也没有将 NamedAccount class 标记为 摘要。因此出现了这个错误。
有两种解决方案:
- 要么将 BankAccount class 声明为抽象的,
- 或者为 getAccountHolderName 和 setAccountHolderName 方法。
注意:@Override 在方法声明旨在覆盖 superclass 中的方法声明时使用。 BankAccount 中的任何方法在 superclass 中都没有相应的方法,所以你不能 @Override.