这两个功能是否过载
class yogi{ public static void fun(){ System.out.println("Fun"); } public void fun(int a,int b){ System.out.println("int"); } }
解决方法
是的,那些是超载.从
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.
对于静态和实例方法具有相同名称是一个好主意是相当罕见的(IMO),但它完全有效.
有趣的是,这可能会导致重载解析出现问题,因为即使没有实例调用方法,也会包含方法.例如:
public class Test { public void foo(String x) { } public static void foo(Object x) { } public static void bar() { foo(""); // Error } }
规范本来可以设计成这样就好了(并调用静态方法)但是它是一个错误:
Test.java:9: error: non-static method foo(String) cannot be referenced from a static context foo(""); ^