了解期中考试样本,使用 println 进行多级考试

发布于 2024-12-11 18:37:57 字数 1177 浏览 0 评论 0原文

大家好,我正在准备期中考试,我正在学习上学期期中考试的样本,答案已经给出,但我试图弄清楚答案是如何产生的。下面的问题和答案,我明白他是如何得到“零”但不明白其余的:

写出程序 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 技术交流群。

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

发布评论

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

评论(3

薄荷港 2024-12-18 18:37:57
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");   // 1. zero
     Silly s = new SillyBird(1);   // 2. zanzibar
     Silly s2 = new Loony();       // 3. zanzibar zircon duchess stupendous
     s.poit(s2);                   // 4. snark
     s2.poit(s);                   // 5. drum zort
     System.out.println("zymurgy");// 6. 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");
   }
}

我希望这会有所帮助...我认为最重要的是要理解第三点,其中有隐式的 super() 调用。

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");   // 1. zero
     Silly s = new SillyBird(1);   // 2. zanzibar
     Silly s2 = new Loony();       // 3. zanzibar zircon duchess stupendous
     s.poit(s2);                   // 4. snark
     s2.poit(s);                   // 5. drum zort
     System.out.println("zymurgy");// 6. 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");
   }
}

I hope this helps... I think the most important one to understand is number 3 where you have implicit super() calls.

找回味觉 2024-12-18 18:37:57

您可能不明白的是,当构造函数没有显式调用 super() 时,编译器无论如何都会在构造函数的开头添加一个 super() 调用。所以,

public SillyBird() {
     System.out.println("duchess");
}

相当于

public SillyBird() {
    super(); 
    System.out.println("duchess");
}

在没有的构造函数的最开始添加 super() 调用,然后跟踪调用,你就会找到答案。例如,调用

Silly s2 = new Loony();

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,

public SillyBird() {
     System.out.println("duchess");
}

is equivalent to

public SillyBird() {
    super(); 
    System.out.println("duchess");
}

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 to

Silly s2 = new Loony();

calls 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 calls this(0). SO the 1-arg constructor of Bird is called, etc.

半透明的墙 2024-12-18 18:37:57

如果您不明白“他如何得到其余的” - 您需要阅读有关对象、类和接口的基本教程。从 Oracle 提供的开始。简而言之,打印语句的发生顺序与构造函数和重写方法的执行顺序相同。例如,打印第二个单词“zanzibar”是因为

public SillyBird(int i) {
    super(i);
}

调用构造函数 Silly s = new SillyBird(1);,它调用:

public Bird(int i) {
    System.out.println("zanzibar");
}

通过 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

public SillyBird(int i) {
    super(i);
}

constructor is invoked Silly s = new SillyBird(1);, which invokes:

public Bird(int i) {
    System.out.println("zanzibar");
}

via the super(i) invocation.

Now try to reason through how the second 'zanzibar' print occurs.

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