可以说 "method overloading",其中两个没有关系(扩展)的 diff 类 具有相同的方法名称但不同类型的参数吗?

Can it be said "method overloading", where two diff classes with no relationship (extends) having same method name but different types of arguments?

我知道 Java 方法可以在同一个 class 或子 class.

中重载

但是我可以在非 subclass 中重载 Java 静态方法吗?

我对下面的代码有点困惑。

class A {   
    public static void main(String[] args) {
        System.out.println("Inside main's (String[]) method");      
    }
}

class B {
    public static void main(int[] args) {  // [1, 2, 3]
        System.out.println("Inside main's (int[]) method");
    }
}

public class _2b_Overloading_Staticmethod_NonSubClass {
    // JVM's entry point
    public static void main(String[] args) {
        A.main(args);
        B.main(new int[]{1,2,3});
    }
}

I am aware of the fact that a Java method can be overloaded in same class or in a subclass.

正确。当同一个 class 中的多个函数共享相同的名称但参数不同时,一个方法被认为是重载的。

public class A 
{

    public void foo(int param)
    {
    }

    public void foo(string param)
    {
    }
}

But can I overload a Java static method in a non-subclass ?

是的,你可以。 subclass 是从父 class 继承属性的 class(也称为 superclass)。如果 class 是 subclass,而不是 subclass 或者方法是静态的,则没有禁止方法重载的规则。

public class A 
{

    public static void foo(int param)
    {
    }

    public static void foo(string param)
    {
    }
}

但是您问题中的上述代码不是方法重载的示例,因为具有相同名称(main)的方法不包含在同一个class/scope中。然而,它们的参数列表确实有所不同,但这并不重要。

此外,一些来自 JLS 的备份,以及一堆示例。

超载

来自JLS § 8.4.9

If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but signatures that are not override-equivalent, then the method name is said to be overloaded.

  • 这对静态成员来说…

    class Foo {
        static int someStaticMethod(String s) {
            return 0;
        }
        static int someStaticMethod(String s, boolean b) {
            return 0;
        }
    }
    
  • …还有non-static个:

    class Foo {
        int someMethod(String s) {
            return 0;
        }
    
        // Method overloading
        int someMethod(String s, boolean b) {
            return 0;
        }
    }
    
  • 如果 class 从另一个 class 继承了一个方法,那么重载也是如此 class:

    class Bar extends Foo {
        // This is also a case of overloading, because Bar inherits
        // someMethod(String) from Foo
        int someMethod(String s, double d, boolean b) {
            return 0;
        }
    }
    

但是

如果两个方法不相关,尽管名称相同,但永远不会被称为重载

  • 两个non-static方法都是这样

    class Alpha {
        void anotherMethod(String s) { }
    }
    class Bravo {
        // No overloading involved
        void anotherMethod(boolean b) { }
    }
    
  • 以及静态方法:

    class Charlie {
        static void someThirdMethod(String s) { }
    }
    class Delta {
        // No overloading involved
        static void someThirdMethod(boolean b) { }
    }
    
  • 对于静态成员,即使 class 中的一个继承了另一个也是如此:

    class Echo {
        static void lastMethod(String s) { }
    }
    class Foxtrot extends Echo {
        // No overloading involved. Static members are never inherited,
        // so effectively, these two methods are unrelated
        static void lastMethod(boolean b) { }
    }
    

More about override-equivalence