日期:2014-05-20  浏览次数:20798 次

java新手提问
下面这个程序运行的结果让人不理解。
运行结果为:
Parent is creating..//不理解
Son is eat: 0//不理解
Son is creating..
Son is eat: 1
主要是前2句不理解。请高手详细说明之。谢谢
Java code

/**
 *Parent.java
 */
public class Parent {
    private int eatID=0;
    public Parent(){
        System.out.println("Parent is creating..");
        eat();
    }
    public void eat(){
        System.out.println("Parent is eat: "+eatID);
    }

}



Java code

/**
 *Son.java
 */
public class Son extends Parent {
    private int eatID=1;
    public Son(){
        System.out.println("Son is creating..");
        eat();
    }
    public void eat(){
        System.out.println("Son is eat: "+eatID);
    }
    @SuppressWarnings(value={"unused"})
    public static void main(String[] args){
        Parent p=new Son();
    }

}



------解决方案--------------------
Java code
Classfile /Son.class
Last modified 2011-6-26; size 373 bytes
MD5 checksum f425bcb4f6d0dbd5e8450c356525a0eb
Compiled from "Son.java"
public class Son extends Parent
  SourceFile: "Son.java"
  minor version: 0
  major version: 50
  flags: ACC_PUBLIC, ACC_SUPER

  Constant pool:
const #1 = Method    #6.#18;    //  Parent."<init>":()V
const #2 = Field    #4.#19;    //  Son.eatID:I
const #3 = Method    #4.#20;    //  Son.eat:()V
const #4 = class    #21;    //  Son
const #5 = Method    #4.#18;    //  Son."<init>":()V
const #6 = class    #22;    //  Parent
const #7 = Asciz    eatID;
const #8 = Asciz    I;
const #9 = Asciz    <init>;
const #10 = Asciz    ()V;
const #11 = Asciz    Code;
const #12 = Asciz    LineNumberTable;
const #13 = Asciz    eat;
const #14 = Asciz    main;
const #15 = Asciz    ([Ljava/lang/String;)V;
const #16 = Asciz    SourceFile;
const #17 = Asciz    Son.java;
const #18 = NameAndType    #9:#10;    //  "<init>":()V
const #19 = NameAndType    #7:#8;    //  eatID:I
const #20 = NameAndType    #13:#10;    //  eat:()V
const #21 = Asciz    Son;
const #22 = Asciz    Parent;

{
public Son();
  flags: ACC_PUBLIC

  Code:
   Stack=2, Locals=1, Args_size=1
   0:    aload_0
   1:    invokespecial    #1; //Method Parent."<init>":()V
   4:    aload_0
   5:    iconst_1
   6:    putfield    #2; //Field eatID:I
   9:    aload_0
   10:    invokevirtual    #3; //Method eat:()V
   13:    return
  LineNumberTable: 
   line 3: 0
   line 2: 4
   line 5: 9
   line 6: 13


public void eat();
  flags: ACC_PUBLIC

  Code:
   Stack=1, Locals=2, Args_size=1
   0:    aload_0
   1:    getfield    #2; //Field eatID:I
   4:    istore_1
   5:    return
  LineNumberTable: 
   line 9: 0
   line 10: 5


public static void main(java.lang.String[]);
  flags: ACC_PUBLIC, ACC_STATIC

  Code:
   Stack=2, Locals=2, Args_size=1
   0:    new    #4; //class Son
   3:    dup
   4:    invokespecial    #5; //Method "<init>":()V
   7:    astore_1
   8:    return
  LineNumberTable: 
   line 13: 0
   line 14: 8


}

Classfile /Parent.class
Last modified 2011-6-26; size 298 bytes
MD5 checksum 3a9133d4b82f26cac2da21e973f55e2e
Compiled from "Son.java"
class Parent extends java.lang.Object
  SourceFile: "Son.java"
  minor version: 0
  major version: 50
  flags: ACC_SUPER

  Constant pool:
const #1 = Method    #5.#15;    //  java/lang/Object."<init>":()V
const #2 = Field    #4.#16;    //  Parent.eatID:I
const #3 = Method    #4.#17;    //  Parent.eat:()V
const #4 = class    #18;    //  Parent
const #5 = class    #19;    //  java/lang/Object
const #6 = Asciz    eatID;
const #7 = Asciz    I;
const #8 = Asciz    <init>;
const #9 = Asciz    ()V;
const #10 = Asciz    Code;
const #11 = Asciz    LineNumberTable;
const #12 = Asciz    eat;
const #13 = Asciz    SourceFile;
const #14 = Asciz    Son.java;
const #15 = NameAndType    #8:#9;    //  "<init>":()V
const #16 = NameAndType    #6:#7;    //  eatID:I
const #17 = NameAndType    #12:#9;    //  eat:()V
const #18 = Asciz    Parent;
const #19 = Asciz    java/lang/Object;

{
public Parent();
  flags: ACC_PUBLIC

  Code:
   Stack=2, Locals=1, Args_size=1
   0:    aload_0
   1:    invokespecial    #1; //Method java/lang/Object."<init>":()V
   4:    aload_0
   5:    iconst_0
   6:    putfield    #2; //Field eatID:I
   9:    aload_0
   10:    invokevirtual    #3; //Method eat:()V
   13:    return
  LineNumberTable: 
   line 20: 0
   line 19: 4
   line 22: 9
   line 23: 13


public void eat();
  flags: ACC_PUBLIC

  Code:
   Stack=1, Locals=2, Args_size=1
   0:    aload_0
   1:    getfield    #2; //Field eatID:I
   4:    istore_1
   5:    return
  LineNumberTable: 
   line 26: 0
   line 27: 5


}