0
public class Account
{
static int accountNum;
static int nextAccountNum = 112200;
static double balance;


    public static void main(String args[])
    {
        Account checkingAccount = new Account(1500.00);
        Account savingAccount = new Account(1000.00);
        Account moneyMarketAccount = new Account(500.00);

        checkingAccount.getAccountNumber();
        savingAccount.getAccountNumber();
        moneyMarketAccount.getAccountNumber();

    }

    public Account(double bal)
    {
        balance = bal;
        accountNum = nextAccountNum;
        nextAccountNum++;
    }

}

每次我运行它时,我都没有得到任何返回变量。我所看到的只是过程完成的文本。寻求帮助

4

3 回答 3

1

将 main 函数与具有多个实例的类分开通常是一种很好的做法。Getter 和 setter 方法用于正确封装类变量,作为帐户可能应该具有的。下面是一个例子。

主.java

public class Main {
    public static void main(String args[])
    {
        Account checkingAccount = new Account(1,1500.00);
        Account savingAccount = new Account(2,1000.00);
        Account moneyMarketAccount = new Account(3,500.00);

        System.out.println("Account " + checkingAccount.getAccountNumber() 
                + " has balance of: " + checkingAccount.getBalance());
        System.out.println("Account " + savingAccount.getAccountNumber() 
                + " has balance of: " + savingAccount.getBalance());
        System.out.println("Account " + moneyMarketAccount.getAccountNumber() 
                + " has balance of: " + moneyMarketAccount.getBalance());
    }
}

账户.java

public class Account
{
private int accountNum;
private int nextAccountNum = 112200;
private double balance;

    public Account(int accountNum, double balance)
    {
        this.balance = balance;
        this.accountNum = accountNum;
    }

    public int getAccountNumber() {
        return accountNum;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(int balance) {
        // check if balance variable is a correct value
        // before setting it to the class variable
        this.balance = balance;
    }

}
于 2013-05-08T22:27:44.120 回答
1

你没有输出任何东西。用于System.out.println输出账户余额,例如

System.out.println(checkingAccount.getAccountNumber());

此外,使您accountNum和您的balance变量为非static,否则余额和帐号将被每个新实例覆盖。

于 2013-05-08T22:15:48.623 回答
0

一个问题是accountNumbalance被声明static。您需要将它们作为实例字段,以便每个实例Account都有自己的值。

你没有得到输出的原因是你的main()方法创建了几个Account对象,但没有对它们做任何事情。

public class Account
{
    static int nextAccountNum = 112200;
    int accountNum;
    double balance;

    public static void main(String args[])
    {
        Account checkingAccount = new Account(1500.00);
        Account savingAccount = new Account(1000.00);
        Account moneyMarketAccount = new Account(500.00);

        checkingAccount.getAccountNumber();
        savingAccount.getAccountNumber();
        moneyMarketAccount.getAccountNumber();

        System.out.println("Checking: " + checkingAccount.toString());    
        System.out.println("Savings: " + savingAccount.toString());    
        System.out.println("Money market: " + moneyMarketAccount.toString());    
    }

    public Account(double bal)
    {
        balance = bal;
        accountNum = nextAccountNum;
        nextAccountNum++;
    }

    @Override
    public String toString() {
        return "Account " + accountNum + ": balance=" + balance;
    }    
}
于 2013-05-08T22:16:04.993 回答