gpt4 book ai didi

java - 参数顺序 - 覆盖

转载 作者:行者123 更新时间:2023-12-04 10:53:03 31 4
gpt4 key购买 nike

根据javadoc

An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.



我的问题是 - 参数的顺序是否无关紧要?如果它们具有相同的名称并且它们也是相同的类型?

最佳答案

顺序很重要,因此具有不同顺序的相同参数的两个方法不被认为具有相同的签名。
例如,这个例子不编译:

interface Foo {
void doIt(String what, int times);
}

class Bar implements Foo {
public void doIt(int times, String what) {}
}

但是,参数的名称无关紧要。这完全没问题:
interface Foo {
void doIt(String what, int times);
}

class Bar implements Foo {
public void doIt(String andNowForSomeThingCompetelyDifferent, int theLarch) {}
}

关于java - 参数顺序 - 覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16177216/

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