Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.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_Methods_Interface - Fatal编程技术网

Java方法签名和接口

Java方法签名和接口,java,methods,interface,Java,Methods,Interface,我们知道,方法签名只包括方法名称和参数列表,而不包括方法返回类型。既然java不区分具有相同签名的方法,那么为什么我会在下面的代码中遇到编译器错误呢 public class InterfaceTest implements I2{ public void hello(){ } public void world(){ } } interface I1{ public int hello(); } interface I2 extends I1{ p

我们知道,方法签名只包括方法名称和参数列表,而不包括方法返回类型。既然java不区分具有相同签名的方法,那么为什么我会在下面的代码中遇到编译器错误呢

 public class InterfaceTest implements I2{

     public void hello(){  }

     public void world(){  }
}

interface I1{
    public int hello();
}

interface I2 extends I1{
  public void world();
}

根据返回类型是声明的一部分。在实现类中,声明必须相同。此外,扩展接口继承父接口的所有方法,因此hellow方法必须与i1中的hellow方法匹配。问题恰恰在于java无法区分具有相同签名(但返回类型不同)的方法

让我们看看在你的情况下会发生什么(假设这是可能的)

您可以执行以下操作:

InterfaceTest obj = new InterfaceTest();
obj.hellow(); // The compiler knows it returns void here.
i1 obj1 = new InterfaceTest();
obj1.hellow(); // The compiler thinks it return an int, when it doesn't return anything.

方法的返回类型是其签名的一部分,您的编译错误是因为您的返回类型与intereface中声明的返回类型不匹配。

您指的是方法
重写
,而不是
重载
。返回类型必须与在超类/接口中的原始重写方法中声明的返回类型相同或是其子类型。

您在这里没有,您是,也有方法,但方式不正确。。。。有两种可能解决您的问题:

public class InterfaceTest implements I2{

    public void hello(int a){  }  // overloaded method

    @Override
    public int hello(){ return 1; }  // overriden method

    public void world(){  }   // this hides I1 method
}
关键是,如果您在单个类中尝试此操作:

public void hello() {}
public int hello() {return 1;}
您将在键入YourClass时得到
replicate method hello()
error,因为对于一个方法,您必须更改签名的
FormalParameterListopt

如果一个类[…]的两个方法具有相同的名称,但签名不是覆盖等效的,则该方法名称被称为重载

最后但并非最不重要的一点:
方法签名只包括方法名称和参数列表,而不包括方法返回类型

根据,声明方法时:

MethodDeclaration:
    MethodHeader MethodBody

MethodHeader:
     MethodModifiersopt TypeParametersopt Result MethodDeclarator Throwsopt

MethodDeclarator:
    Identifier ( FormalParameterListopt )
所以当你这样做的时候:

  public int hellow(int number) throws Exception;
//|      |   |      |           └ throwing an exception (Throwsopt)
//|      |   |      └──────────── receiving one int argument (MethodDeclarator FormalParameterListopt )
//|      |   └─────────────────── name hellow (MethodDeclarator Identifier)
//|      └─────────────────────── returning an int (Result)
//└────────────────────────────── is a public method (MethodModifiersopt)

您正在类中实现一个接口,该类的方法名称与接口“hello()”相同,因此有两种情况

1) 如果您正在实现一个接口,那么在这种情况下,您应该编写其方法的具体实现

  • 公共int hello()
  • 公共虚空世界() 但您只提供了一种方法的实现

    • “公共空间世界()”

      因此,它将要求您实现另一个方法,它将成为这个类的一部分

    2) 当您提供public void world()方法的具体实现时,它将与您的方法冲突public void hello(),现在,由于被重写的方法成为类的成员,并且您的方法具有相同的参数列表,因此它将无法满足重载条件

    因此,这将使覆盖和重载功能都失败。

    @Override
    注释类“InterfaceTest”中的
    world()
    方法。但它仍然会声明您必须重写
    int hellow()