Java &引用;找不到符号:方法";但是方法是声明的

Java &引用;找不到符号:方法";但是方法是声明的,java,Java,在我的驱动程序中,这一行给了我找不到符号的错误,我不知道为什么。该方法在SavingsAccount类中有明确定义,我可以引用驱动程序中的所有其他方法,但不是那个方法,我尝试将类型更改为double,等等,但仍然不起作用 Account acct2 = new SavingsAccount (name); acct2.calculateBalance(); SavingsAccount类继承自Account类: public class SavingsAccount extends Accou

在我的驱动程序中,这一行给了我
找不到符号的错误,我不知道为什么。该方法在
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);

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

而且您的
帐户
类中没有
calculateBalance()
方法

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

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

SavingsAccount acct2 = new SavingsAccount (name);
或者,您可以在访问该方法时显式强制转换它

((SavingsAccount) acct2).calculateBalance();
但请注意,如果
acct2
对象实际上不是
SavingsAccount的对象,它可能会抛出
ClassCastException

更新:

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

尝试切换

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

或者(不知道为什么),我相信你可以将
acct2
转换为
SavingsAccount
“所有其他方法”都来自
帐户,而不是
SavingsAccount
。当您想调用
cCalculateBalance
时,可以这样尝试:

(SavingsAccount)acct2.calculateBalance()

我认为多态性引用给了我这样做的能力,因为你已经在
Account
类中定义了
draw
方法,虽然它是抽象的,但我想我还没有完全掌握这个概念checked@Aaron查看我的更新,您应该清楚关于多态性的概念方法calculateBalance()完全属于阶级储蓄账户,也就是说,你没有超越它。要使用代码,工作父类帐户应该声明或实现它。
SavingsAccount acct2 = new SavingsAccount (name);
acct2.calculateBalance();
(SavingsAccount)acct2.calculateBalance()