Rss & SiteMap

课外天地 李树青 http://www.njcie.com

李树青 论坛 南京 财经 课外天地
共3 条记录, 每页显示 10 条, 页签: [1]
[浏览完整版]

标题:[原创]Java中方法重写注意事项!

1楼
信管07151 发表于:2010/4/5 15:11:33

李老师,您好!

下面一题怎么做?谢谢!

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是错的权限缩小了。

其他选项还有那个是对的?为什么?

2楼
admin 发表于:2010/4/6 13:34:00

我觉得你应该自己上机试一试,不就知道结果了吗?

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

3楼
信管07151 发表于:2010/4/7 17:35:49
谢谢!李老师!
共3 条记录, 每页显示 10 条, 页签: [1]

Copyright ©2002 - 2016 课外天地.Net
Powered By Dvbbs Version 8.3.0
Processed in .03125 s, 2 queries.