简体   繁体   English

“找不到符号:方法”但声明了方法

[英]“cannot find symbol: method” but the method is declared

In my driver program, this line gives me cannot find symbol error and I don't know why. 在我的驱动程序中,这行让我cannot find symbol错误,我不知道为什么。 The method is clearly defined in the SavingsAccount class, and I can refer to all other methods in my driver program but just not that one, I tried changing the type to double , and etc but still not working. 该方法在SavingsAccount类中明确定义,我可以参考我的驱动程序中的所有其他方法但不是那个,我尝试将类型更改为double ,等等但仍然无效。

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

SavingsAccount class inherits from Account class: 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;
    }


}

Account class, if needed 帐户类,如果需要的话

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

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

This is because although you have an object of SavingsAccount you are using refrence variable of type Account so you can access only those methods that are there in Account class. 这是因为虽然您有一个SavingsAccount对象,但您使用的是类型为Account引用变量,因此您只能访问Account类中的那些方法。

And you don't have calculateBalance() method in your Account class. 并且您的Account类中没有calculateBalance()方法。

That's why you are not able to access it and compiler complains that it cannot find a method named calculateBalance as it sees that reference type is Account and there is no such method inside Account class. 这就是为什么你无法访问它并且编译器抱怨它无法找到名为calculateBalance的方法,因为它看到引用类型是Account并且Account类中没有这样的方法。

If you want to use that method then change reference type to SavingsAccount : 如果要使用该方法,请将引用类型更改为SavingsAccount

SavingsAccount acct2 = new SavingsAccount (name);

Or you can explicitly cast it when accessing that method 或者您可以在访问该方法时显式地转换它

((SavingsAccount) acct2).calculateBalance();

but be alert that it can throw ClassCastException if acct2 object is actually not an object of SavingsAccount 但请注意,如果acct2对象实际上不是SavingsAccount的对象,它可以抛出ClassCastException

UPDATE: 更新:

But remember that at runtime, Java uses virtual method invocation to dynamically select the actual version of the method that will run, based on the actual instance. 但请记住,在运行时,Java使用虚拟方法调用来根据实际实例动态选择将运行的方法的实际版本。

Try switching, 尝试切换,

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

To

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

Or (Not sure why you would want to), I believe you could cast acct2 to SavingsAccount . 或者(不确定为什么你会这样),我相信你可以将acct2投射到SavingsAccount

"All other methods" are coming from Account , not SavingsAccount . “所有其他方法”来自Account ,而不是SavingsAccount When you are wanting to call cCalculateBalance , try it like this: 当您想要调用cCalculateBalance ,请尝试这样:

(SavingsAccount)acct2.calculateBalance()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM