gpt4 book ai didi

java - 尝试覆盖方法时奇怪的 Eclipse 行为

转载 作者:搜寻专家 更新时间:2023-11-01 01:49:38 24 4
gpt4 key购买 nike

<分区>

我有这两个类,DogBeagle 扩展了 Dog

class Dog {
protected String bark() {return "woof "; }
}
class Beagle extends Dog {
private String bark() { return "arf "; }
}
class Test {
public static void main(String args[]) {
Dog[] dogs = {new Dog(), new Beagle()};
for(Dog d : dogs)
System.out.print(d.bark());
}
}

当我使用任何其他编辑器而不是 Eclipse 时,上面的代码甚至无法编译。我收到此错误:

Attempting to assign weaker access privileges to bark() method in Beagle class.

您还可以看到此行为 here .

如果我使用 Eclipse Indigo(版本:3.7.2),这段代码可以正常编译,输出为:woof woof

请让我明白哪一个是正确的,为什么?

提前致谢!

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