gpt4 book ai didi

Java protected 方法的歧义类型

转载 作者:太空宇宙 更新时间:2023-11-04 06:59:56 26 4
gpt4 key购买 nike

我在 Eclipse 中遇到以下错误消息:

The method getString(String, BasicNameValuePair[]) is ambiguous for the type Class1

对于以下代码:

public final class Class1 {
public static void getString(String requiredArguments, BasicNameValuePair[] dataPairs) {

}

protected static void getString(String requiredArguments, byte[] dataBytes) {

}
}

public final class Class2 {
public static void callTest() {
Class1.getString("This is a test", null);
}
}

callTest()中的代码将导致上述错误。

很明显,null 可以转换为 BasicNameValuePair[] 以及 byte[],但请注意此处的 protected 修饰符。以 byte[] 作为参数的方法对于 Class2 根本不应该可见,因此不会有歧义。

为什么 protected 方法对其他(非子类)类可见并导致此错误?

最佳答案

因为这些类位于同一个包中?

The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

关于Java protected 方法的歧义类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22236132/

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