Green mock 3 第11题

herben 2003-01-26 02:41:18
What will happen when you attempt to compile and run the following code
class Base{
public void Base(){
System.out.println("Base");
}
}
public class In extends Base{
public static void main(String argv[]){
In i=new In();
}
}
1) Compile time error Base is a keyword
2) Compilation and no output at runtime
3) Output of Base
4) Runtime error Base has no valid constructor
ANSWER:
2) Compilation and no output at runtime
Because the method in Base called Base has a return type it is not a constructor and there for does not get called on creation of an instance of its child class In

我认为没有可选项,因为In和Base都未定义Constructor
...全文
20 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
coolsand 2003-02-28
  • 打赏
  • 举报
回复
楼上的,人家看看题没有错啊,我觉得这道题就是想让初学者认为public void Base(){……}是个构造函数,你再牛,也不该打击别人学习的积极性吗!
xiaofenguser 2003-01-26
  • 打赏
  • 举报
回复
2
Base和In都有Constructor.
对于原程序里没有定义Constructor的.java文件,编译的时候会给加上去默认的空Constructor的.
经过编译后的结果会是这样:
import java.io.PrintStream;
class Base
{
Base()
{
}
public void Base()
{
System.out.println("Base");
}
}
public class In extends Base
{
public In()
{
}
public static void main(String args[])
{
In in = new In();
}
}
Northwindrocker 2003-01-26
  • 打赏
  • 举报
回复
楼主看来你的JAVA还要多看书现在还不是作MOCK的时候啊!这个问题考点根本不是关于构造函数的你居然问出了这么高深的问题我倒!JAVA的构造函数当你没有自己写一个的时候系统会为你隐藏添加一个默认构造函数的!

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧