Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 实现在接口和父类中声明的方法_Java_Inheritance - Fatal编程技术网

Java 实现在接口和父类中声明的方法

Java 实现在接口和父类中声明的方法,java,inheritance,Java,Inheritance,我有点进退两难 说我有课 public class Child extends Parent implements Interface{ public static void main(String[] args){ Child child = new Child(); child.methodOne(); } } 在哪里 此结构没有编译错误 如果是我干的呢 abstract class Parent { public void methodOne() throws Except

我有点进退两难

说我有课

public class Child extends Parent implements Interface{
 public static void main(String[] args){
  Child child = new Child();
   child.methodOne(); } }
在哪里

此结构没有编译错误

如果是我干的呢

abstract class Parent {

public void methodOne() throws Exception {
    System.out.print("cannot fly ");
}}

interface Interface {
public abstract void methodOne(); }
两个顺从的错误上升说

Exception Exception in throws clause of Parent.methodOne() is not compatible with Interface.methodOne()

它要求在
main
方法中添加
throws
try/catch


为什么会发生这种情况?

抛出的
声明不是接口
接口
中的
methodOne()
契约的一部分

无论何时实现接口,都必须实现方法契约,如果接口具有
throws
声明,则无需添加
throws
声明即可实现

但是,您不能添加
界面
合同中未说明的功能

您可以通过修改
main
方法来修复另一个错误,以同时引发
异常

 public static void main(String[] args) throws Exception {
  Child child = new Child();
   child.methodOne(); 
 }
或者在
try catch
块中包围
methodOne()
调用:

 public static void main(String[] args) throws Exception {
  Child child = new Child();
   try{
       child.methodOne(); 
   }catch(Exception e){
      //do something to handle exception i.e. System.out.println(e.getMessage());
   }
 }
补充阅读:

摘录:

实现和扩展的一般规则是,可以使新类或接口“限制较少”,但不能“限制更多”。如果您将处理异常的需求视为一种限制,那么不声明异常的实现就没有那么严格了。任何编写接口代码的人都不会在您的类中遇到问题


抛出
声明不是接口
接口
中的
methodOne()
契约的一部分

无论何时实现接口,都必须实现方法契约,如果接口具有
throws
声明,则无需添加
throws
声明即可实现

但是,您不能添加
界面
合同中未说明的功能

您可以通过修改
main
方法来修复另一个错误,以同时引发
异常

 public static void main(String[] args) throws Exception {
  Child child = new Child();
   child.methodOne(); 
 }
或者在
try catch
块中包围
methodOne()
调用:

 public static void main(String[] args) throws Exception {
  Child child = new Child();
   try{
       child.methodOne(); 
   }catch(Exception e){
      //do something to handle exception i.e. System.out.println(e.getMessage());
   }
 }
补充阅读:

摘录:

实现和扩展的一般规则是,可以使新类或接口“限制较少”,但不能“限制更多”。如果您将处理异常的需求视为一种限制,那么不声明异常的实现就没有那么严格了。任何编写接口代码的人都不会在您的类中遇到问题


每次实现接口(或重写父类中的方法,同样的事情)时,都需要遵守该接口的约定。遵守契约意味着您不能抛出比接口声明中更多的异常

在这两种情况下,
接口
是契约,
父级
是实现(通过类
子级
中发生的两者之间的“绑定”)

在第一种情况下,接口表示允许
methodOne()
抛出异常,但不必这样做。您在
Parent
中的实际实现不会抛出任何东西,因此它是合同的一个子集,可以

在第二种情况下,接口表示
methodOne()
不会抛出任何(*),因此任何实现都不允许抛出任何异常。但是,
Parent
中的实现要求允许抛出,这与接口契约不兼容;因此出现了编译器错误

还要注意,在第二种情况下,您可以很好地使用空的
抛出
声明覆盖
Child
中的
methodOne()
,以便签名符合接口约定。然后可以调用
super.methodOne()
并将
Exception
重写为
RuntimeException
(例如)

顺便说一下:

  • 如果使用相同的方法签名实现多个接口,则实现只能抛出所有接口的
    声明的交集
    
  • 您不需要在接口中使用
    abstract
    关键字,因为接口中的方法声明本质上总是抽象的


(*)当然除了RuntimeException。

每次实现接口(或重写父类中的方法,同样的事情)时,都需要遵守该接口的约定。遵守契约意味着您不能抛出比接口声明中更多的异常

在这两种情况下,
接口
是契约,
父级
是实现(通过类
子级
中发生的两者之间的“绑定”)

在第一种情况下,接口表示允许
methodOne()
抛出异常,但不必这样做。您在
Parent
中的实际实现不会抛出任何东西,因此它是合同的一个子集,可以

在第二种情况下,接口表示
methodOne()
不会抛出任何(*),因此任何实现都不允许抛出任何异常。但是,
Parent
中的实现要求允许抛出,这与接口契约不兼容;因此出现了编译器错误

还要注意,在第二种情况下,您可以很好地使用空的
抛出
声明覆盖
Child
中的
methodOne()
,以便签名符合接口约定。然后可以调用
super.methodOne()
并将
Exception
重写为
RuntimeException
(例如)

顺便说一下:

  • 如果使用相同的方法签名实现多个接口,则实现只能抛出所有接口的
    声明的交集
    
  • 你不需要使用
    public class Child  extends Parent implements Interface