简体   繁体   English

Java覆盖抽象接口方法

[英]Java Override Abstract Interface Method

I have a BankAccount.java that implements NamedAccount.java which is an abstract interface, but it keeps giving me an error that since BankAccount.java is not abstract it can't override it. 我有一个实现NamedAccount.java的BankAccount.java,这是一个抽象接口,但是它一直给我一个错误,因为BankAccount.java不是抽象的,所以无法覆盖它。 How do I fix this problem? 我该如何解决这个问题? I tried adding @Override in both situations but it does not work! 我试图在两种情况下都添加@Override,但是它不起作用!

BankAccount.java: 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: NamedAccount.java:

 public interface NamedAccount {
   String getAccountHolderName();
   void setAccountHolderName(final String theNewName);
 } 

and to make it run you'll need this SafeDepositBoxAccount.java: 为了使其运行,您将需要以下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;
       }
     }

The error is telling you that you must override those methods (from your interface) because the class isn't abstract . 错误提示您,因为类不是abstract所以必须 (从您的接口)重写这些方法。 If you make the class abstract then you wouldn't be required to override the methods. 如果将类抽象化,则无需重写这些方法。 Something like, 就像是,

private String accountHolderName;
@Override
public String getAccountHolderName() {
    return accountHolderName;
}
@Override
public void setAccountHolderName(final String theNewName) {
    this.accountHolderName = theNewName;
}

Since your class BankAccount says it implements NamedAccount , you need to implement both the menthod's defined in the contract as mentioned here ie setAccountHolderName and getAccountHolderName in your BankAccount class or else as compiler says you need to define your class as an abstract class, so other class can extend your abstract class and define those two method to form a concrete class. 由于您的班级BankAccount表示它实现了NamedAccount ,因此您需要实现合约中定义的方法,如此处提到的即BankAccount类中的setAccountHolderNamegetAccountHolderName ,否则编译器表示您需要将您的类定义为抽象类,因此其他类可以扩展您的抽象类并定义这两种方法以形成具体的类。

If you want to get rid of the compilation error, then you need to override those two methods just like what you defined in SafeDepositBoxAccount class: 如果要摆脱编译错误,则需要重写这两个方法,就像在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;
    }
 }

In your code, NamedAccount is an interface and any class that implements an interface ( BankAccount and SafeDepositBoxAccount in this case) MUST implement all of the interface's methods except if the class is declared to be abstract . 在您的代码中,NamedAccount是一个接口,任何实现该接口的类(在这种情况下, 此类BankAccountSafeDepositBoxAccount )都必须实现该接口的所有方法,除非该类被声明为abstract

For BankAccount class, neither have the implementations of getAccountHolderName and setAccountHolderName been provided, nor has NamedAccount class been marked as abstract . 对于BankAccount类,既未提供getAccountHolderName和setAccountHolderName的实现,也未将NamedAccount类标记为abstract Therefore this error has occurred. 因此,发生此错误。

There are two solutions: 有两种解决方案:

  1. Either declare BankAccount class to be abstract, 要么将BankAccount类声明为抽象类,
  2. Or write the implementations for both getAccountHolderName and setAccountHolderName methods. 或编写getAccountHolderName和setAccountHolderName方法的实现。

NOTE: @Override is used when a method declaration is intended to override a method declaration in a superclass. 注意:@Override用于方法声明旨在覆盖超类中的方法声明时。 Any method in BankAccount does not have corresponding method in the superclass so you cannot @Override. BankAccount中的任何方法在超类中都没有对应的方法,因此您不能@Override。

public abstract class BankAccount implements NamedAccount {

    abstract String getAccountHolderName();

    abstract void setAccountHolderName(final String theNewName);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM