李老师,您好!
下面一题怎么做?谢谢!
Adds to the Beta class to make it compile correctly.
class Alpha{
public void bar(int ... x){ }
public void bar(int x){ }
}
public class Beta extends Alpha{
Place here code;
Place here code;
Place here code;
}
选项 A private void bar (int x){ }
B public void bar(int x){ }
C public int bar (String x){return 1;}
D public Alpha bar(int x){ }
E public void bar (int x,int y){ }
F public int bar (int x){ return x;}
老师我知道B和E是对的,A是错的权限缩小了。
其他选项还有那个是对的?为什么?
我觉得你应该自己上机试一试,不就知道结果了吗?
A权限缩写,报错(Cannot reduce the visibility of the inherited method from Alpha) B正确,乃函数重写 C正确,此非函数重写,应该是重载 D错误,重写函数要求原型一致,重载函数要求返回类型一致(The return type is incompatible with Alpha.bar(int)) E正确,此非函数重写,应该是重载 F错误,理由同D