계정 이름으로 클래스를 만들었습니다. 그런 다음 유형 클래스의 객체를 인스턴스화합니다. 모든 코드는 TestAccount 파일에 저장됩니다. testaccount.TestAccount.main (TestAccount.java:8) 에서 "주"스레드에서클래스를 인스턴스화 할 수 없습니다.
예외 아래 java.lang.ExceptionInInitializerError 을 표시에 의해 발생하지만 시스템은 나에게 오류를 제공합니다 : java.lang.RuntimeException가를 : Uncompilable 소스 코드 - 클래스 계정 공개, testaccount.Account에서 Account.java 라는 이름의 파일에 선언해야한다 (TestAccount.java:20) ... (1 개) 더 자바 결과 : 1
. 아래는 내 코드입니다.
package testaccount;
public class TestAccount
{
public static void main(String[] args)
{
Account Account1=new Account();
Account1.setId(1122);
Account1.setBalance(20000);
Account1.setAnnualInterestRate(4.5);
System.out.println("The monthly interest rate is " + Account1.getMonthlyInterestRate());
System.out.println("The balance after the withdrawal is "+ Account1.withdraw(2000));
System.out.println("The balabce after the deposit is " + Account1.deposit(3000));
}
}
public class Account
{
private int id;
private double balance;
private double annualInterestRate;
private static long dateCreated;
public Account()
{
id=0;
balance=0;
annualInterestRate=0;
dateCreated=System.currentTimeMillis();
}
public Account(int newId,double newBalance)
{
id=newId;
balance=newBalance;
}
public int getId()
{
return id;
}
public void setId(int newId)
{
id=newId;
}
public double getbalance()
{
return balance;
}
public void setBalance(double newBalance)
{
balance=newBalance;
}
public double getAnnualInterestRate()
{
return annualInterestRate;
}
public void setAnnualInterestRate(double newAnnualInterestRate)
{
annualInterestRate=newAnnualInterestRate;
}
public static long getDateCreate()
{
return dateCreated;
}
public double getMonthlyInterestRate()
{
return (annualInterestRate/12);
}
public double withdraw(double newWithdraw)
{
return (balance-newWithdraw);
}
public double deposit(double deposit)
{
return (balance+deposit);
}
}
내가 잘못하고있는 것을 말해 줄 수 있습니까?
단일 Java 파일에 2 개의 분리 된 공용 클래스를 사용할 수 없습니다. –
오류 메시지는 다음과 같은 힌트를 제공합니다 :'class Account is public, Account.java라는 파일에 선언되어야 함' – ajp15243
그러면 두 파일을 어떻게 연결 할 수 있습니까? Account.java와 Testaccount.java? – user2918968