这两个功能是否过载
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(""); ^
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。