2015-04-27 71 views
0

我需要的程序是使用帳戶類創建測試帳戶程序。以下是我想出了:需要幫助瞭解特定的未解決的編譯錯誤

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; 
} 

} 

} 

這裏是我得到的錯誤:在線程

異常「主要」 java.lang.Error的:未解決的問題,編譯: 類型沒有外圍實例TestAccount可以訪問。必須用封閉的TestAccount類型的實例來限定分配(例如,x.new A(),其中x是TestAccount的一個實例)。

在accounts.TestAccount.main(TestAccount.java:12)

請幫助我明白我需要做的。謝謝。

回答

0

你有一個嵌套類。在當非靜態類嵌套在非靜態類的new聲明需要像錯誤消息Java裏面說:

TestAccount ta = new TestAccount(); 
TestAccount.Account ta_a = ta.new Account(); 

語法是醜陋的再次購買,於是 - 嵌套類。

有關更多信息,請參見documentation

希望這會有所幫助。

+0

非常感謝。 – Mokonalove

+0

它使用帳戶firstaccount = new TestAccount()。new Account()。 – Mokonalove