问题描述
在我的驱动程序中,这行给了我找不到符号
错误,我不知道为什么。该方法在 SavingsAccount
类中明确定义,我可以参考我的驱动程序中的所有其他方法但不是那个,我尝试将类型更改为 double
等等但仍无效。
In my driver program, this line gives me cannot find symbol
error and I don't know why. 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.
Account acct2 = new SavingsAccount (name);
acct2.calculateBalance();
SavingsAccount
class继承自帐户
类:
SavingsAccount
class inherits from Account
class:
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);
}
推荐答案
Account acct2 = new SavingsAccount (name);
acct2.calculateBalance();
这是因为你有一个对象您正在使用
Account
类型的引用变量,因此您只能访问 Account
类中的那些方法。
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.
您的
帐户中没有
calculateBalance()
方法c $ c>上课。
And you don't have
calculateBalance()
method in your Account
class.
这就是为什么你无法访问它并且编译器抱怨它找不到名为
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.
如果您想使用该方法,请将参考类型更改为
SavingsAccount
:
If you want to use that method then change reference type to
SavingsAccount
:
SavingsAccount acct2 = new SavingsAccount (name);
或者您可以在访问该方法时显式转换它
Or you can explicitly cast it when accessing that method
((SavingsAccount) acct2).calculateBalance();
但要警惕它可以抛出
ClassCastException
如果 acct2
对象实际上不是的对象,SavingsAccount
but be alert that it can throw
ClassCastException
if acct2
object is actually not an object of SavingsAccount
更新:
但是
请记住,在运行时,Java使用虚拟方法调用动态
select将根据实际实例运行的方法的实际版本。
这篇关于“找不到符号:方法”但是声明了这个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!