0

因此,我在 Java 的高级继承中有一个任务,并且代码几乎已经敲定,但是在完成它并使其实际显示输出时遇到了问题。该程序分为 4 个不同的部分,我将它们列在下面。

账户.java

public abstract class Account 
{
protected long accountNumber;
protected double accountBalance;
public long getAccountNumber()
{
    return accountNumber;
}
public double getAccountBalance()
{
    return accountBalance;
}
public void setAccountNumber(long number)
{
    number = accountNumber;
}
public void setAccountBalance(double balance)
{
    balance = accountBalance;
}
public void setUpAccount(long number, double  balance) //look up constructors
{
    number = accountNumber;
    balance = accountBalance;
}
public String toString()
{
    return "Account Number: " + accountNumber + " Account Balance: " + accountBalance;
}
public abstract double computeInterest(int intPeriod);
}

Checkings.java

public class Checking extends Account
{
public String toString()
{
    String info =  "Checking\nAccount Number: " + accountNumber + 
                                                   "\nAccount Balance: " + accountBalance ;
    return info;
}
public Checking(long number)
{
    number = accountNumber;
}
public double computeInterest(int intPeriod) 
{
    double total = ((accountBalance - 700) * .02) * 3;
    return total;
}
}

储蓄.java

public class Savings extends Account
{
private double interestRate;
public double getInterest()
{
    return interestRate;
}
public void setInterest(double inter)
{
    inter = interestRate;
}
public String toString()
{
    String info = "Savings\nAccount Number: " + accountNumber + " \nAccount Balance: " 
                                                          + accountBalance + " \nInterest Rate: " + interestRate;
    return info;
}
public Savings(long number, double interest)
{
    number = accountNumber;
    interest = interestRate;
}
public double computeInterest(int intPeriod) 
{
    double total = Math.pow((1 + interestRate), intPeriod) * accountBalance - accountBalance;
    return total;   
}
}

AccountArray.java

public class AccountArray 
{   
    public static void main(String[] args)
    {
        Account[] refAccount = new Account[10];
        /*refAccount[0] = new Checking(100);
        refAccount[1] = new Checking(101);
        refAccount[2] = new Checking(102);
        refAccount[3] = new Checking(103);
        refAccount[4] = new Checking(104);
        refAccount[5] = new Savings(105, .02);
        refAccount[6] = new Savings(106, .02);
        refAccount[7] = new Savings(107, .02);
        refAccount[8] = new Savings(108, .02);
        refAccount[9] = new Savings(109, .02);

        refAccount[0].setAccountBalance(1000.0);
    refAccount[1].setAccountBalance(2000.0);
    refAccount[2].setAccountBalance(3000.0);
    refAccount[3].setAccountBalance(4000.0);
    refAccount[4].setAccountBalance(5000.0);
    refAccount[5].setAccountBalance(6000.0);
    refAccount[6].setAccountBalance(7000.0);
    refAccount[7].setAccountBalance(8000.0);
    refAccount[8].setAccountBalance(9000.0);
    refAccount[9].setAccountBalance(10000.0);*/
    for(int inc = 0; inc < 10; inc++ )
    {
        refAccount[0] = new Checking(100);
        refAccount[1] = new Checking(101);
        refAccount[2] = new Checking(102);
        refAccount[3] = new Checking(103);
        refAccount[4] = new Checking(104);
        refAccount[5] = new Savings(105, .02);
        refAccount[6] = new Savings(106, .02);
        refAccount[7] = new Savings(107, .02);
        refAccount[8] = new Savings(108, .02);
        refAccount[9] = new Savings(109, .02);

        refAccount[0].setAccountBalance(1000.0);
        refAccount[1].setAccountBalance(2000.0);
        refAccount[2].setAccountBalance(3000.0);
        refAccount[3].setAccountBalance(4000.0);
        refAccount[4].setAccountBalance(5000.0);
        refAccount[5].setAccountBalance(6000.0);
        refAccount[6].setAccountBalance(7000.0);
        refAccount[7].setAccountBalance(8000.0);
        refAccount[8].setAccountBalance(9000.0);
        refAccount[9].setAccountBalance(10000.0);
    }
    for(int ctr  = 0; ctr < 10; ctr++)
    {
        System.out.println(refAccount[ctr].toString());
    }
}

}

有什么我喜欢的东西真的很明显失踪了吗?这一切都编译得很好,但输出只显示 0 而不是帐号、余额或利息。

任何帮助将不胜感激,因为我的时间不多了。

4

2 回答 2

3

你所有的setter方法看起来都是错误的,你把它们当作......

public void setAccountNumber(long number)
{
    number = accountNumber;
}
public void setAccountBalance(double balance)
{
    balance = accountBalance;
}

你所做的只是设置参数值,它们应该是......

public void setAccountNumber(long number)
{
    accountNumber= number;
}
public void setAccountBalance(double balance)
{
    accountBalance= balance;
}

你可以确保这不会发生我让你的参数最终

于 2013-04-07T13:24:57.763 回答
1

看看你的构造函数和设置器你反转你的变量属性

前任

public void setAccountNumber(long number)
{
    number = accountNumber;
}

应该

public void setAccountNumber(long number)
{
    accountNumber = number;
}

你所有的设置器和构造器都一样

于 2013-04-07T13:25:58.340 回答