gpt4 book ai didi

java - 余额在 SavingsAccount1 中具有私有(private)访问权限,并且不允许我在FlexibleSavingsAccount1 中访问它

转载 作者:行者123 更新时间:2023-12-02 05:20:04 24 4
gpt4 key购买 nike

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 帐户结束

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

最佳答案

正确。改变这个

private double balance;

protected double balance;

以便子类可以直接访问它。或者,您可以使用 setBalance(double)getBalance() 代替。

关于java - 余额在 SavingsAccount1 中具有私有(private)访问权限,并且不允许我在FlexibleSavingsAccount1 中访问它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26580739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com