简体   繁体   English

Java无法弄清楚要调用的方法

[英]Java can not figure out the method to be invoked

I haven't found the reason ( in JLS ) why java compiler can't chouse appropriate method to execute in following program: 我没有找到原因( 在JLS中 )为什么java编译器无法在下面的程序中执行适当的方法来执行:

public class UpperLevelClass {
    private static String getStringanotherNameMethod(String a, String b) {
        return null;
    }

    private static String firstSignatureMethod(String a, String b) {
        return null;
    }

    static class StaticNestedClass extends UpperLevelClass{
        public void getStringfirstNameMethod(String a, Integer b) {
            getStringanotherNameMethod("test", "fff");//compiles
            firstNameMethod("test", "fff");//error below
        }
    }
}

Compiling finishes with following error: 编译完成并出现以下错误:

error: method firstNameMethod in class StaticNestedClass cannot be applied to given types;
            firstNameMethod("test", "fff");
            ^
  required: String,Integer
  found: String,String
  reason: argument mismatch; String cannot be converted to Integer

Adding some history: 添加一些历史:

At first I had several static method with default access in upper level calls and everething compiles and runs(from static nested class) fine. 起初我有几个static方法,在上层调用中使用default访问,并且everething编译和运行(来自静态嵌套类)很好。 Then static methods access was changed to private and programm stops compiling due to error in one method (in this case firstNameMethod), but other methods compiles fine. 然后static方法访问被更改为private并且程序停止编译由于一个方法中的错误(在本例中为firstNameMethod),但其他方法编译正常。


I have tried(thx to @ Jorn Vernee ) to compile and run programm in Eclipse and it compiles and runs. 我已经尝试(thx to @ Jorn Vernee )在Eclipse中编译和运行programm并编译并运行。

Searching for the appropriate method is a two-step process. 搜索适当的方法分为两步。

Step 1 involves selecting the class to be searched. 步骤1涉及选择要搜索的类。 The relevant line of the JLS of this step: 此步骤的JLS的相关行:

If the Identifier appears in the scope of a visible method declaration with that name: 如果标识符出现在具有该名称的可见方法声明的范围内:

If there is an enclosing type declaration of which that method is a member, let T be the innermost such type declaration. 如果存在该方法是成员的封闭类型声明,则让T为最内层的类型声明。 The class or interface to search is T. 要搜索的类或接口是T.

Note that it says Identifier and not Signature here. 请注意,它在此处显示标识符而非签名 This means that it is only looking at the method name at this stage, and not the arguments. 这意味着它只在此阶段查看方法名称,而不是参数。 Because StaticNestedClass contains a method called getString , that is the class to be searched. 因为StaticNestedClass包含一个名为getString的方法,即要搜索的类。

It is only when we get to Step 2 that arguments are taken into account. 只有当我们到达第2步时,才会考虑参数。 Because there is no getString method in StaticNestedClass that are compatible with the method call, this leads compilation to fail. 因为StaticNestedClass中没有与方法调用兼容的getString方法, StaticNestedClass这会导致编译失败。

Because your outer class' static method is private , your inner class can't see the declaration. 因为你的外部类的静态方法是private ,所以你的内部类不能看到声明。 Removing the private keyword is enough to ensure that the method can actually be seen. 删除private关键字足以确保实际可以看到该方法。

Alternatively, the fully qualified name could also be used - that is, invoke the method with UpperLevelClass.getString("test", "fff"); 或者,也可以使用完全限定名称 - 也就是说,使用UpperLevelClass.getString("test", "fff");调用该方法UpperLevelClass.getString("test", "fff"); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM