我需要的程序是使用帐户类创建一个测试帐户程序。以下是我的想法:
package accounts;
import java.util.Date;
public class TestAccount {
public static void main(String[] args) {
// TODO Auto-generated method stub
java.util.Date date = new java.util.Date();
Account firstaccount = new Account (1111, 10000.00, 0.045);
System.out.println("Transaction started: " + date.toString());
System.out.println("User's ID is: " + firstaccount.getId());
System.out.println("User's balance is: " + firstaccount.getBalance());
System.out.println("The montlhly interest rate is: " + firstaccount.getMonthlyInterestRate());
System.out.println("Balance after withdraw is: " + firstaccount.withdraw(1000));
System.out.println("Balance after deposit is: " + firstaccount.deposit(3000));
System.out.println("Transaction complete."); }
class Account {
private int id = 0;
private double balance = 0.0;
private double annualInterestRate = 0.0;
public Account (int newId, double newBalance, double newAnnualInterestRate) {
id = newId;
balance = newBalance;
annualInterestRate = newAnnualInterestRate;
}
public int getId() {
return id;
}
public double getBalance () {
return balance;
}
public double getAnnualInterestRate () {
return annualInterestRate;
}
public double getMonthlyInterestRate () {
return annualInterestRate/12;
}
public double withdraw (double value) {
return balance -= value;
}
public double deposit (double value) {
return balance += value;
}
public void setId (int newId) {
id = newId;
}
public void setBalance (double newBalance) {
balance = newBalance;
}
public void setAnnualInterestRate (double newAnnualInterestRate) {
annualInterestRate = newAnnualInterestRate;
}
}
}
以下是我遇到的错误:
线程“main”中的异常java.lang.Error:未解决的编译问题: 不能访问类型为TestAccount的封闭实例。必须使用TestAccount类型的封闭实例限定分配(例如x.new A(),其中x是TestAccount的实例)。
at accounts.TestAccount.main(TestAccount.java:12)
请帮助我理解我需要做什么。谢谢。
答案 0 :(得分:0)
你有一个嵌套类。在java中,当非静态类嵌套在非静态类中时,new
语句需要像错误消息所示:
TestAccount ta = new TestAccount();
TestAccount.Account ta_a = ta.new Account();
语法难看再买 - 嵌套类也是如此。
有关详细信息,请参阅documentation。
希望这有帮助。