cod*_*dy1 1 java exception super
我有一个自定义异常类,如下所示:
public abstract class AccountException extends BankServiceException {
private int accountNo;
public AccountException(String message, int accountNo) {
    super(message);
    this.accountNo = accountNo;
}
public int getAccountNo() {
    return accountNo;
}
现在我需要将这个抽象类扩展到一个更具体的异常:
public class  OverdraftLimitReachedException extends AccountException {
private double amount;
private double overdraft;
public OverdraftLimitReachedException(int accountNo,double amount,double overdraft) {
 super("The overdraft limit has been exceded",accountNo,amount,overdraft);
    this.overdraft = overdraft;
    this.amount = amount;
}
public double getAmount() {
    return amount;
}
public double getOverdraft() {
    return overdraft;
}
现在这里的问题是超级构造函数,我知道我需要给它参数数量和透支,但是当我尝试编译它时,它说参数的长度不同。我如何正确调用超级构造函数,所以我可以给出错误信息。谢谢!
编辑:我添加了构造函数的实际外观(对不起,我有点笨拙)。稍后在代码中,当我实际抛出异常时,我需要提供构造函数的参数。
在您的代码中,您使用以下命令调用超构造函数:
super("The overdraft limit has been exceded",accountNo,amount,overdraft);
这传递了 4 个参数:
"The overdraft limit has been exceded"( String)accountNo( int)amount( double)overdraft( double)你的超级构造函数是这样写的:
public AccountException(String message, int accountNo) {
这接受 2 个参数:
Stringint您需要使两个参数列表匹配。您需要将调用 super 编辑为:
super("The overdraft limit has been exceded", accountNo);
或者您需要将超级构造函数编辑为:
public AccountException(String message, int accountNo, double amount, double overdraft) {
我怀疑您想使用前者,因为您已经使用amount并overdraft使用以下代码:
this.overdraft = overdraft;
this.amount = amount;
另外,一个小错误,但您在邮件中拼错了“超出”一词。