balance在SavingsAccount1中具有私人访问权限,并且不允许我在FlexibleSavingsAccount1中访问它

时间:2014-10-27 03:20:08

标签: java inheritance polymorphism private

public abstract class SavingsAccount1
//public static double annualInterestRate;
private double balance;
private final int ACCOUNT_NUMBER;{

public SavingsAccount1(){
    this.balance = 0.0;
    this.ACCOUNT_NUMBER =0;
}
public SavingsAccount1(int ACCOUNT_NUMBER, double balance)
{
this.balance = balance;
this.ACCOUNT_NUMBER = ACCOUNT_NUMBER;
}
public abstract void addMonthlyInterest();

public double getBalance()
{   return balance;
}
public void setBalance(double balance){
this.balance = balance; 
}

public int getAccount_Number()
{
return ACCOUNT_NUMBER;
}

}

public class FlexibleSavingsAccount1 extends SavingsAccount1{

//private double addInterest;
public static double annualInterestRate;

public FlexibleSavingsAccount1 (int ACCOUNT_NUMBER, double balance){

    super(ACCOUNT_NUMBER, balance);

    //this.annualInterestRate = annualInterestRate;
    //this.balance = balance;
    //this.addInterest = addInterest;

}//end of 

In this method I am trying to override, it says that balance has private Access in 

SavingsAccount1,并且不允许我访问它的信息。

@Override public void addMonthlyInterest(){

    balance =balance +(balance * annualInterestRate / 12);
}

} // FlexibleSavings帐户结束

我已经尝试了我能想到的一切,所以如果代码看起来很糟糕,我会提前道歉。我一直在改变它,我甚至不知道该做什么。你们提供的任何帮助都会很棒!

2 个答案:

答案 0 :(得分:1)

正确。改变这个

private double balance;

protected double balance;

这样子类就可以直接访问它。或者,您也可以使用setBalance(double)getBalance()

答案 1 :(得分:1)

声明为private的方法和变量只能在声明的类本身中访问。

受保护的访问修饰符允许您访问同一类及其所有子类中的变量。