SCJP Sierra Bates 第 2 章问题 2 默认构造函数调用

发布于 2024-11-01 14:55:23 字数 939 浏览 0 评论 0原文

背景信息 我对 Sierra & 的问题有疑问。贝茨,SCJP v6 书。即第2章问题2。给出的答案是“编译失败”。然而,当我在 neBeans 中尝试此操作时,代码编译并运行时没有错误。它还返回了“D”输出,这不是替代方案之一。在各个论坛上还有一些关于同一问题的其他讨论,涉及是否需要插入 super() 等。但是似乎没有人认识到它可以编译。

问题 1.我期望构造函数“Bottom2(String s)...调用超级构造函数“Top(String s)...”。在这种情况下,输出将是“BD”(这恰好是一个选项为什么“Top(String s)...”没有被调用。 2. 由于存在 Top 构造函数,那么仍然会隐式创建默认的编译器构造函数。即实际上是一个“Top() {}”构造函数,可以通过“Bottom2(String s)”调用。这不是我理解这种情况发生的方式 - 即编译器仅在没有创建其他构造函数的情况下创建此默认值。 3. 这个问题是否存在错误,或者这是 Java 5 版本中遗留的问题,在 Java 6 中编译器现在可以以某种方式处理这个问题。 4. netBeans 是否有办法“解决”编译器问题。这非常重要,因为我正在学习 SCJP,而且我发现并非所有问题都可以在 netBeans 中重复。在这种情况下,我可能会学会相信某些代码在(出于考试目的)不起作用时可以起作用。

包含代码以方便参考。

class Top { 
    public Top(String s) { System.out.print("B"); } 
} 

public class Bottom2 extends Top { 
    public Bottom2(String s) { System.out.print("D"); } 
    public static void main(String [] args) { 
        new Bottom2("C"); 
        System.out.println(" "); 
    }
}

Background info
I have a query regarding a questions from Sierra & Bates, SCJP v6 book. Namely Chapter 2 question 2. The answer given is that the "compilation fails". However when I tried this in neBeans, the code compiled and ran without error. It also returned a output of "D" which was not one of the alternatives. There are some other discussions on this same question in various forums, regarding the need to insert super() etc. However none seem to have recognised it can compile.

Question
1. I expected the constructor "Bottom2(String s)...to have called the super constructor "Top(String s)...". In which case the output would have been "BD" (which happens to be an option for the question. Why does the "Top(String s)..." not get called.
2. As there is a Top constructor then would the default compiler constructor still be implicitly created. ie in effect a "Top() {}" constructor which can be called by "Bottom2(String s)". This not how I understood this to happen - ie the compiler only creates this default if no other constructor is created.
3. Is there and error in this question, or is this a carry over question from the Java 5 version and somehow in Java 6 the compiler can now handle this.
4. Could netBeans have a means to "solve" the compiler problem. This is quite important as I am studying for the SCJP and I find not all the questions can be duplicated in netBeans. In which case I may learn to believe some code works when (for exam purposes) it does not.

Code included for ease of reference.

class Top { 
    public Top(String s) { System.out.print("B"); } 
} 

public class Bottom2 extends Top { 
    public Bottom2(String s) { System.out.print("D"); } 
    public static void main(String [] args) { 
        new Bottom2("C"); 
        System.out.println(" "); 
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分开我的手 2024-11-08 14:55:23

Top 没有默认构造函数(默认构造函数是带有空参数列表的公共构造函数。因此,Bottom2 的构造函数必须明确调用超级构造函数(并传递其参数),但没有,因此编译失败,

事实上, eclipse helios 说:

隐式超级构造函数 Top() 未定义。必须显式调用另一个构造函数

并且 javac 说:

找不到符号
符号:构造函数 Top()
位置:类工具.顶部
    公共 Bottom2(String s) { System.out.print("D"); }
                             ^

您真的确定在 Netbeans 中尝试过相同的代码吗?

Top doesn't have a default constructor (a default constructor is a public constructor with empty argument list. Therefore, the constructor of Bottom2 must explicitly invoke the super constructor (and pass its argument), but doesn't, and hence compilation fails.

Indeed, eclipse helios says:

Implicit super constructor Top() is undefined. Must explicitly invoke another constructor

and javac says:

cannot find symbol
symbol  : constructor Top()
location: class tools.Top
    public Bottom2(String s) { System.out.print("D"); }
                             ^

Are you really sure you have tried the same code in Netbeans?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文