【问题标题】:Java Account program - No Enclosing Instance of Type is AccessibleJava 帐户程序 - 没有可访问的封闭类型实例
【发布时间】:2016-02-14 14:20:42
【问题描述】:

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

Exercise10_7 类型的封闭实例不可访问

我该如何解决这个问题?此外,完成此操作后,我需要对其进行修改以容纳 10 个帐户的数组,其中包含 4 个选项菜单(检查余额、取款、存款、退出)。如果我能得到一些指导来完成这件事,那就太棒了。

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

【问题讨论】:

标签: java arrays class methods


【解决方案1】:

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

class Account {

static class Account {

改变

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

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

JLS-8.1.3. Inner Classes and Enclosing Instances 说(部分)

内部类是一个嵌套类,没有显式或隐式声明static

【讨论】:

  • 非常感谢。那么显式声明是“静态类 Account”,而隐式声明是“new Exercise10_7().new Account”?
【解决方案2】:

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

  1. 让你的内心阶层static
  2. 从你的类的对象访问你的内部类。例如YourInnerClass obj = new OuterClass(). new YourInnerClass (parameters)

【讨论】:

    猜你喜欢
    • 2012-09-10
    • 2013-09-12
    • 1970-01-01
    • 2011-12-15
    • 2016-11-20
    • 2013-04-11
    • 2012-06-17
    • 1970-01-01
    相关资源
    最近更新 更多