类使用相同的方法实现两个接口但检查异常不同 [英] Class implements two interfaces with the same method but different checked exceptions

查看:94
本文介绍了类使用相同的方法实现两个接口但检查异常不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SG Ganesh和Tushar Sharma的书Oracle认证专业Java SE 7程序员考试1Z0-804和1Z0-805 A综合OCPJP 7认证指南一书中,说明了



如果一个方法在两个或更多个接口中被声明,并且如果该方法
声明在throws子句中抛出不同的异常,则
实现应该列出所有这些例外(第347页)


那么我试过这个

  public interface I1 {
public void x()throws I1Exception;
}

public interface I2 {
public void x()throws I2Exception;
}

public class I1Exception extends Exception {
}

public class I2Exception extends Exception {
}

public class示例实现I1,I2 {

@Override
public void x()throws I2Exception {
throw new I2Exception();
}

@Override
public void x()throws I1Exception {
throw new I1Exception();
}

}

当然,异常不是方法签名的一部分,所以编译器抱怨第二种方法是重复的。另一方面,



  @Override 
public void x()throws I1Exception,I2Exception {
throw new I1Exception();
}

与两个接口不兼容,因为它声明比任何一个每个接口的方法分别单独使用。



这意味着我不明白这本书是说什么,还是意味着这本书的陈述是荒谬的? / strong>

解决方案

该书的具体句子似乎不正确。 Java语言规范有更合理的条款解释这个问题(我认为是这本书的部分)。


当涉及到界面时,可能会覆盖多个方法声明通过单独的首字母声明。在这种情况下,重写声明必须具有与所有重写的声明(§9.4.1)兼容的throws子句。


所以在这种情况下,与两个声明兼容的一个是

  @Override 
public void x(){
}

在这两个声明中都有常见的例外,他们可能包含在宣言。

  interface I1 {
public void x()throws I1Exception,I2Exception;
}

接口I2 {
public void x()throws I2Exception;
}

然后

  @Override 
public void x()throws I2Exception {
}

或者在您的示例中,如果您有类似于

 类I1Exception extends I2Exception {
}

class I2Exception extends Exception {
}

然后一个兼容的方法声明可以写成

  @Override 
public void x()throws I1Exception {

}


In the book "Oracle Certified Professional Java SE 7 Programmer Exams 1Z0-804 and 1Z0-805 A Comprehensive OCPJP 7 Certification Guide" by S G Ganesh and Tushar Sharma, it's stated

"if a method is declared in 2 or more interfaces, and if that method declares to throw different exceptions in the throws clause, the implementation should list all these exceptions" (page 347)

Well, then I've tried this

public interface I1 {
    public void x() throws I1Exception;
}

public interface I2 {
    public void x() throws I2Exception;
}

public class I1Exception extends Exception {
}

public class I2Exception extends Exception {
}

public class Sample implements I1, I2{

    @Override
    public void x() throws I2Exception {
        throw new I2Exception();        
    }

    @Override
    public void x() throws I1Exception {
        throw new I1Exception();        
    }

}

Of course, the exception is not part of the method signature, so the compiler complains that the second method is duplicated.

On the other hand

@Override
public void x() throws I1Exception, I2Exception {
    throw new I1Exception();        
}

Is not compatible with both interfaces because it declares more checked exceptions than any one of the methods of each interface, taken separately.

This means that I don't understood what the book says or does that mean the book statement is innacurate?

解决方案

That specific sentence of the book seems to be incorrect. The Java Language Specification has more reasonable clause explaining this issue (which I think is the part referred by the book).

When interfaces are involved, more than one method declaration may be overridden by a single overriding declaration. In this case, the overriding declaration must have a throws clause that is compatible with all the overridden declarations (§9.4.1).

So in this case, one that is compatible with both declarations would be

@Override
public void x() {
}

In both declarations had common exceptions they could have included in the declaration.

interface I1 {
    public void x() throws I1Exception, I2Exception;
}

interface I2 {
    public void x() throws I2Exception;
}

then

@Override
public void x() throws I2Exception {
}

Or alternatively in your example, if you had something like

class I1Exception extends I2Exception {
}

class I2Exception extends Exception {
}

Then a compatible method declaration could be written as

@Override
public void x() throws I1Exception {

}

这篇关于类使用相同的方法实现两个接口但检查异常不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆