【发布时间】:2011-05-15 04:45:16
【问题描述】:
如果我要导入的类与我要导入的类在同一个包中,我是否应该导入一个 java 类(我正在尝试扩展前一个类, SavingAccount 和 SpecialSavings 都在同一个帐户中)?此外,我正在尝试从子类访问父类中的私有变量,但它不会让我这样做。这是我的代码:
package finance;
import finance.SavingsAccount;
public class SpecialSavings extends SavingsAccount
{
public SpecialSavings(double savingsBalance)
{
super.setBalance(savingsBalance);
}
if (super.getBalance() > 10000)
{
modifyInterestRate(10);
}
}
这是 SavingsAccount 的代码:
package finance;
public class SavingsAccount
{
private static double annualInterestRate;
private double savingsBalance;//indicates the amount the saver currently has on deposit
public SavingsAccount(double savingsBalance)//default constructor
{
this.savingsBalance = savingsBalance;
}
public double calculateMonthlyInterest()
{
savingsBalance += savingsBalance * ((annualInterestRate/100)/12);
return savingsBalance;
}
public static void modifyInterestRate(double newValue)
{
annualInterestRate = newValue;
}
/*-------------getters and setters------------------------*/
public void setBalance(double newValue)
{
savingsBalance = newValue;
}
public double getBalance()
{
return savingsBalance;
}
}
【问题讨论】:
-
显示
SavingsAccount的代码,如果是这样,也可以将其添加为标签。