方法检索和继承混淆

时间:2012-01-14 19:14:29

标签: java class inheritance methods private-methods

好的,所以我遇到了很多麻烦,我还在学习Java,我的书为我设定了一个我在网上常见的任务,我坚持的部分是...

我必须创建一个银行帐户计划,帐户持有人会获得一个储蓄帐户(具有利率且没有透支额度),以及一个支票帐户(具有100英镑且不感兴趣的超支设施)。

我还没有实施透支,只是获得退出和存款功能准备的一半但我的问题是有兴趣的,我已经在我的超类中定义了储蓄账户余额和支票账户余额,所以在锻炼时我对储蓄账户类的兴趣我不能参考savebalance,因为我把它变成了私有的。我正在尝试使用set.name方法,但我显然做错了....

一个大大的微笑,感谢任何可以提供帮助或提供建议的人!

超类如下:

public class BankDetails
    {
        private String customer;
        private String accountno;
        private double savebalance;
        private double checkbalance;


//Constructor Methods

 public BankDetails(String customerIn, String accountnoIn, double savebalanceIn, double checkbalanceIn)
        {
            customer = customerIn;
            accountno = accountnoIn;
            savebalance = savebalanceIn;
            checkbalance = checkbalanceIn;
        }

      // Get  name
      public String getcustomername()
        {
            return (customer);
        }

      // Get account number
      public String getaccountnumber()
        {
            return (accountno);
        }

       public double getcheckbalanceamount()
        {
            return (checkbalance);
        }

       public double getsavebalanceamount()
        {
            return (savebalance);
        }


public void savewithdraw(double savewithdrawAmountIn)

  {
        savebalance = savebalance - savewithdrawAmountIn;

  }

public void checkwithdraw(double checkwithdrawAmountIn)

  {
        checkbalance = checkbalance - checkwithdrawAmountIn;

  }


 public void savedeposit(double savedepositAmountIn)

  {
        savebalance = savebalance - savedepositAmountIn;

  }

public void checkdeposit(double checkdepositAmountIn)

  {
        checkbalance = checkbalance - checkdepositAmountIn;

  }




    } // End Class BankDetails

子类如下:

    import java.util.*; 

public class Savings extends BankDetails
  {

      private String saveaccount;
      private double interest;


      public Savings(String customerIn, String accountnoIn, float interestIn, 
      String saveaccountIn, double savebalanceIn)
        {


            super (customerIn, accountnoIn, savebalanceIn, interestIn);


            saveaccount = saveaccountIn;
            interest = interestIn;

        }


      public String getsaveaccountno()
       {
           return (saveaccount);
       }

      public double getinterestamount()
       {
           return (interest);
       }


      public void interestamount(String[] args)

       {
           BankDetails.getsavebalanceamount(savebalance);
           interest = (savebalance / 100) * 1.75;


       }


       }

2 个答案:

答案 0 :(得分:3)

使用超类的getSaveBalance()方法来访问余额(由于您有一个储蓄帐户类,但在其他地方保留余额,这是可疑的名称。)

(目前它是getsavebalanceamount(),我假设重命名以保持Java惯例。)


我建议在命名getter和setter时使用一致的CamelCase,例如getInterestAmount()getSaveAccountNo()等。

我建议不要评论简单的getter / setter,但如果你这样做,请使用Javadoc约定,例如:

/** Returns current savings account balance. */
public double getSaveBalance() { ... etc ... }

我还建议避免使用不必要的括号,例如目前在你的getter中,例如:

public double getSaveBalance() {
    return saveBalance; // No parens required.
}

答案 1 :(得分:2)

我建议你做这样的事情,

interface Account{
  int getAccountNumber();
  float getBalance();
}

public class SavingAccount implements Account, Interest{
  int accountNumber;
  public int getAccountNumber(){
    return accountNumber;
  }
  float balance;
  public float getBalance(){
    return balance;
  }
  float savingInterestRate;
  public float getInterestRate(){
     return savingInterestRate;
  }
}

public class CheckingAccount implements Account, OverDraft{
  int accountNumber;
  public int getAccountNumber(){
  return accountNumber;
  }
  float balance;
  public float getBalance(){
    return balance;
  }
}

interface Interest{
  float getInterestRate();
}

interface OverDraft{
....
}