Java帐户程序-无法访问类型为的封闭实例

Java帐户程序-无法访问类型为的封闭实例,java,arrays,class,methods,Java,Arrays,Class,Methods,第07行-我得到以下错误: 无法访问Exercise10_7类型的封闭实例 我如何解决这个问题?此外,完成后,我需要修改它持有一个4选项菜单(检查余额,提取,存款,退出)10个帐户数组。如果我能得到一些指导来实现这一点,那将是非常棒的 import java.util.Date; public class Exercise10_7 { public static void main(String[] args) { Account account1 = new Accoun

第07行-我得到以下错误:

无法访问Exercise10_7类型的封闭实例

我如何解决这个问题?此外,完成后,我需要修改它持有一个4选项菜单(检查余额,提取,存款,退出)10个帐户数组。如果我能得到一些指导来实现这一点,那将是非常棒的

import java.util.Date;

public class Exercise10_7 {

    public static void main(String[] args) {

    Account account1 = new Account(1122, 20000, .045);
    account1.withdraw(2500);
    account1.deposit(3000);
    java.util.Date dateCreated = new java.util.Date();

    System.out.println("Date Created:" + dateCreated);
    System.out.println("Account ID:" + account1.id);
    System.out.println("Balance:" + account1.getBalance());
    System.out.println("Interest Rate:" + account1.getAnnualInterestRate());
    System.out.println("Balance after withdraw of 2500:" +       account1.getAnnualInterestRate());
    System.out.println("Balance after deposit of 3000:" + account1.getAnnualInterestRate());
    System.out.println("Monthly Interest:" + account1.id);
    System.out.println("Process completed.");
    }

    class Account {
        //define variables
        private int id;
        private double balance; // balance for account
        private double annualInterestRate; //stores the current interest rate
        private Date dateCreated; //stores the date account created

        //no arg construtor
        Account () {
            id = 0;
            balance = 0.0;
            annualInterestRate = 0.0;
        }
        //constructor with specific id and initial balance
        Account(int newId, double newBalance) {
            id = newId;
            balance = newBalance;
        }
        Account(int newId, double newBalance, double newAnnualInterestRate) {
            id = newId;
            balance = newBalance;
            annualInterestRate = newAnnualInterestRate;
        }
        //accessor/mutator methods for id, balance, and annualInterestRate
        public int getId() {
            return id;
        }
        public double getBalance() {
            return balance;
        }
        public double getAnnualInterestRate() {
            return annualInterestRate;
        }
        public void setId(int newId) {
            id = newId;
        }
        public void setBalance(double newBalance) {
            balance = newBalance;
        }
        public void setAnnualInterestRate(double newAnnualInterestRate) {
            annualInterestRate = newAnnualInterestRate;
        }
        //accessor method for dateCreated
        public void setDateCreated(Date newDateCreated) {
            dateCreated = newDateCreated;
        }
        //define method getMonthlyInterestRate
        double getMonthlyInterestRate() {
            return annualInterestRate/12;
        }
        //define method withdraw
        double withdraw(double amount) {
            return balance -= amount;
        }   
        //define method deposit
        double deposit(double amount) {
            return balance += amount;   
        }
    }
}

因为
Account
是一个内部类,所以需要一个封闭类的实例来体现它。或者您可以将
Account
设置为静态类。你可以改变

class Account {

更改

Account account1 = new Account(1122, 20000, .045);

说(部分)

内部类是未显式或隐式声明为静态的嵌套类


您无法访问静态方法中的非静态字段/属性,
main
方法是静态的,而您的内部类是非静态的。你有两个选择

  • 使您的内部类成为静态的
  • 从类的对象访问内部类。e、 g
    YourInnerClass obj=new OuterClass()。新建YourInnerClass(参数)

  • 非常感谢你。所以显式声明为“静态类帐户”,隐式声明为“新的Exercise10_7()。新帐户”?可能重复的
    Account account1 = new Account(1122, 20000, .045);
    
    Account account1 = new Exercise10_7().new Account(1122, 20000, .045);