了解期中考试样本,使用 println 进行多级考试
大家好,我正在准备期中考试,我正在学习上学期期中考试的样本,答案已经给出,但我试图弄清楚答案是如何产生的。下面的问题和答案,我明白他是如何得到“零”但不明白其余的:
写出程序 Bird.java 的 10 行输出,如下所示。
interface Silly {
public void narf();
public void poit(Silly s);
}
public class Bird implements Silly {
public static void main(String args[]) {
System.out.println("zero");
Silly s = new SillyBird(1);
Silly s2 = new Loony();
s.poit(s2);
s2.poit(s);
System.out.println("zymurgy");
}
public Bird() {
this(0);
System.out.println("zircon");
}
public Bird(int i) {
System.out.println("zanzibar");
}
public void narf() {
System.out.println("zort");
}
public void poit(Silly s) {
s.narf();
}
}
class SillyBird extends Bird {
public SillyBird() {
System.out.println("duchess");
}
public SillyBird(int i) {
super(i);
}
public void narf() {
System.out.println("drum");
super.narf();
}
}
class Loony extends SillyBird {
public Loony() {
System.out.println("stupendous");
}
public void narf() {
System.out.println("snark");
}
}
他的答案是:零 桑给巴尔 桑给巴尔 锆石 公爵夫人 惊人的 斯纳克 鼓 佐尔特 酶制剂
Hey everyone I'm studying for a midterm exam and I'm studying the sample midterm from a previous semester, the answers are given but I'm trying to figure out how the answers came about. Question and answers below, I understand how he got "zero" but not the rest:
Write the 10-lines output of the program Bird.java shown below.
interface Silly {
public void narf();
public void poit(Silly s);
}
public class Bird implements Silly {
public static void main(String args[]) {
System.out.println("zero");
Silly s = new SillyBird(1);
Silly s2 = new Loony();
s.poit(s2);
s2.poit(s);
System.out.println("zymurgy");
}
public Bird() {
this(0);
System.out.println("zircon");
}
public Bird(int i) {
System.out.println("zanzibar");
}
public void narf() {
System.out.println("zort");
}
public void poit(Silly s) {
s.narf();
}
}
class SillyBird extends Bird {
public SillyBird() {
System.out.println("duchess");
}
public SillyBird(int i) {
super(i);
}
public void narf() {
System.out.println("drum");
super.narf();
}
}
class Loony extends SillyBird {
public Loony() {
System.out.println("stupendous");
}
public void narf() {
System.out.println("snark");
}
}
His answers are: zero
zanzibar
zanzibar
zircon
duchess
stupendous
snark
drum
zort
zymurgy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我希望这会有所帮助...我认为最重要的是要理解第三点,其中有隐式的 super() 调用。
I hope this helps... I think the most important one to understand is number 3 where you have implicit super() calls.
您可能不明白的是,当构造函数没有显式调用 super() 时,编译器无论如何都会在构造函数的开头添加一个
super()
调用。所以,相当于
在没有的构造函数的最开始添加
super()
调用,然后跟踪调用,你就会找到答案。例如,调用Loony
构造函数,该构造函数调用 super()。因此,SillyBird 无参数构造函数被调用,它首先调用 super()。因此,Bird 无参数构造函数被调用,它调用this(0)
。所以 Bird 的 1-arg 构造函数被调用,等等。WHat you probably don't get is that whan a constructor doesn't explicitely invoke super(), then the compiler adds a
super()
call anyway, to the very beginning of the constructor. So,is equivalent to
Add the
super()
calls at the very beginning of the constructors which don't have one, then follow the calls, and you'll find the answer. For example, the call tocalls the
Loony
constructor, which calls super(). So the SillyBird no-arg constructor is called, which first calls super(). So the Bird no-arg constructor is called, which callsthis(0)
. SO the 1-arg constructor of Bird is called, etc.如果您不明白“他如何得到其余的” - 您需要阅读有关对象、类和接口的基本教程。从 Oracle 提供的开始。简而言之,打印语句的发生顺序与构造函数和重写方法的执行顺序相同。例如,打印第二个单词“zanzibar”是因为
调用构造函数
Silly s = new SillyBird(1);
,它调用:通过
super(i)
调用。现在尝试推理第二个“zanzibar”打印是如何发生的。
If you don't understand how 'he got the rest' - you need to read a basic tutorial on objects, classes, and interfaces. Start with the one provided by Oracle. In a nutshell, the print statements occur in the same order the constructors and override methods are being executed. For example, the second word 'zanzibar' is printed because the
constructor is invoked
Silly s = new SillyBird(1);
, which invokes:via the
super(i)
invocation.Now try to reason through how the second 'zanzibar' print occurs.