gpt4 book ai didi

java - 方法参数顺序的改变能叫方法重载吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:16:19 30 4
gpt4 key购买 nike

请注意,在下面的示例中仅更改了参数的顺序。所以我的问题是 - 我们可以调用下面的示例是方法重载吗?

public void show(String s, int a){
System.out.println("Test.show(String, int)");
}
public void show(int s, String a){
System.out.println("Test.show(int, String)");
}

最佳答案

是的,这绝对是方法重载。

来自 section 8.4.9 of the JLS :

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.

“覆盖等效”在 section 8.4.2 中描述:

Two methods have the same signature if they have the same name and argument types.

[ ... Details on "same argument types ... ]

The signature of a method m1 is a subsignature of the signature of a method m2 if either:

  • m2 has the same signature as m1, or

  • the signature of m1 is the same as the erasure (§4.6) of the signature of m2.

Two method signatures m1 and m2 are override-equivalent iff either m1 is a subsignature of m2 or m2 is a subsignature of m1.

关于java - 方法参数顺序的改变能叫方法重载吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18598325/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com