10

在我的驱动程序中,这一行给了我cannot find symbol错误,我不知道为什么。该方法在SavingsAccount类中明确定义,我可以引用驱动程序中的所有其他方法,但不是那个,我尝试将类型更改为double, 等等,但仍然无法正常工作。

Account acct2 = new SavingsAccount (name);
acct2.calculateBalance();

SavingsAccount类继承自Account类:

public class SavingsAccount extends Account
{
    private final short minBalance = 0;
    private double overdraftFee;
    private double yearlyInterestRate = 0.02;
    private double interestAmount;

    public SavingsAccount (String name)
    {
        super(name);
    }

    public double withdraw (double amount)
    {
        if (accountBalance - amount >= minBalance)
        {
            accountBalance -= amount;
            System.out.print ("Withdraw Successful");
        }
        else 
        {
            accountBalance -= amount;
            overdraftFee = accountBalance * (0.10);
            accountBalance += overdraftFee;
            System.out.print ("Withdraw Succesful, however overdraft fee of 10% has been applied to your account");


        }

        return accountBalance;
    }

// ----------------- this is the method I try to invoke -----------------------------
    public void calculateBalance ()
    {
        interestAmount = (accountBalance * yearlyInterestRate);
        accountBalance += interestAmount;
    }
// ----------------------------------------------------------------------------------

    public String toString()
    {
        return super.toString() + " Interest Received: " + interestAmount;
    }


}

帐户类,如果需要

import java.util.Random;
import java.text.NumberFormat;

public abstract class Account
{
    protected double accountBalance;
    protected long accountNumber;
    protected String accountHolder;
    public Account (String name)
    {
        accountHolder = name;
        accountBalance = 0;
        Random accountNo = new Random();
        accountNumber  = accountNo.nextInt(100000);
    }

    public double deposit (double amount)
    {
        accountBalance += amount;

        return accountBalance;
    }

    public String toString()
    {
        NumberFormat accountBal = NumberFormat.getCurrencyInstance();
        return "Account Balance: " + accountBal.format(accountBalance) + "\nAccount Number: " + accountNumber;
    }

    public String getAccountHolder()
    {
        return accountHolder;
    }

    public double getAccountBalance()
    {
        return accountBalance;
    }

    public abstract double withdraw (double amount);

}
4

3 回答 3

10
Account acct2 = new SavingsAccount (name);
acct2.calculateBalance();

这是因为尽管您有一个对象,但SavingsAccount您正在使用类型的引用变量,Account因此您只能访问Account类中存在的那些方法。

而且你的课堂上没有calculateBalance()方法。Account

这就是为什么您无法访问它并且编译器抱怨它找不到名为的方法calculateBalance ,因为它看到引用类型是Account并且类中没有这样的方法Account

如果您想使用该方法,请将引用类型更改为SavingsAccount

SavingsAccount acct2 = new SavingsAccount (name);

或者您可以在访问该方法时显式转换它

((SavingsAccount) acct2).calculateBalance();

但请注意,ClassCastException如果acct2object 实际上不是SavingsAccount

更新:

但请记住,在运行时,Java 使用虚拟方法调用来根据实际实例动态选择将要运行的方法的实际版本。

于 2012-11-21T04:53:04.177 回答
1

换个试试

Account acct2 = new SavingsAccount (name);
acct2.calculateBalance();

SavingsAccount acct2 = new SavingsAccount (name);
acct2.calculateBalance();

或者(不知道你为什么想要),我相信你可以acct2投到SavingsAccount.

于 2012-11-21T04:54:28.160 回答
0

“所有其他方法”来自Account,而不是SavingsAccount。当你想打电话cCalculateBalance时,试试这样:

(SavingsAccount)acct2.calculateBalance()
于 2012-11-21T04:57:41.440 回答