×

Loading...

Topic

This topic has been archived. It cannot be replied.
  • 工作学习 / IT杂谈 / Jabber,I need your help again,thanks ahead!
    I am learning thinking in Java,the following is an example in the book
    class Egg2
    {
    protected class Yolk
    {
    public Yolk()
    {
    System.out.println("Egg2.Yolk()");
    }

    public void f()
    {
    System.out.println("Egg2.Yolk.f()");
    }
    }
    private Yolk y=new Yolk();
    public Egg2()
    {
    System.out.println("New Egg2()");
    }
    public void insertYolk(Yolk yy)
    {
    y=yy;
    }
    public void g()
    {
    y.f();
    }
    }

    public class BigEgg2 extends Egg2
    {
    public class Yolk extends Egg2.Yolk
    {
    public Yolk()
    {
    System.out.println("BigEgg2.Yolk()");
    }
    public void f()
    {
    System.out.println("BigEgg2.Yolk.f()");
    }
    }
    public BigEgg2()
    {
    insertYolk(new Yolk());
    }
    public static void main(String[] args)
    {
    Egg2 e2=new BigEgg2();
    e2.g();
    }
    }
    the result is :
    Egg2.Yolk()
    New Egg2()
    Egg2.Yolk()
    BigEgg2.Yolk()
    BigEgg2.Yolk.f()
    I do not know why the second Egg2.yolk() appears,can you explain the example briefly to me ,thank you very much!!!
    • Here we go
      本文发表在 rolia.net 枫下论坛1) BigEgg2 is the son (subclass) of the Eggs (superclass). BUT, BUT, BUT, BigEgg2 has no idea about "y", because "y" is private in Egg2. Private variables cannot be inherited.

      2) The constructor BigEgg2() { insertYolk( new Yolk) )} means

      public BigEgg2() {
      super();
      insertYolk();
      }

      In any constructor, the first thing is to call super constructor. Since the father is Egg2, super() means Egg2(); Remind::::::: one may
      suppress super() in the above but the compiler will add it.

      4) Now let's go up to check the definition of Egg2. It has ONE, ONE, and only ONE (private) instance variable called "y". To call Egg2(), JVM needs to first create an Egg2. To create an Egg2, JVM first creates a Yolk named "y". This is done a la Yolk(), so you see the first line : Egg2.Yolk

      5) After a Yolk is created, JVM calls Egg2() to do "intialization". As a consequence, you see the second line: New Egg2()

      6) Now we come to insertYolk( new Yolk()).
      Here we create a Yolk using the defintion in the BigEgg2. Again, the compiler will add a super() to the constructor of Yolk. As a result,
      the Yolk constructor is actually

      public Yolk(){
      super();
      System.out.println("BigEgg2.Yok()");
      }
      Here, super() means Yolk() in the Egg2.
      You see, super() prints out the second "Egg2.Yolk()", which is what you tried to understand.

      7) At this stage, you have finished executing the very first line in you main(String[] args) method. Congratulations!!!!

      8) New let's come to the second as well last line in your main(String[] args) method.
      We don't overrride method g(), so we use g() in the Egg2, which is equivalent to y.f(). HOWEVER, HOWEVER, HOWEVER, Method f() has been overridden, so JVM uses the defintion in BigEgg2 and we see last line: BigEgg2.Yolk().f()

      9) Summary. To create an instance of a subclass, we always first create one according to the definition in the super class, then we make some modiftions.
      If we don't call super in the constructor, the compiler may do that for us. This process can be traced back to java.lang.Object.


      10) please pay me one buck because I find I lost one hair for answering your question.
      The design of this Egg class is awful. In practice, we should avoid such things. But to learn concepts, Bruce Eckel's book is really good.更多精彩文章及讨论,请光临枫下论坛 rolia.net