class Parent {
  public Parent() {
    System.out.println(this);
  }
  static void test() {
    System.out.println(this);
  }
}
class Child extends Parent {
  public static void main(String[] args) {
    new Child();
  }
}
在第三行打印this,打印结果怎么显示this是Child的对象的引用呢?
不解,求dx!

解决方案 »

  1.   

    我认为应该是Parent,因为在new Child()时,同时也产生一个Parent的对象。但是结果证明我是错的,不知道原理是什么,所以求救。
      

  2.   

    healer_kx(甘草{决心把CSDN改造成全国最大的文学爱好者的社区}) ( ) 信誉:100    Blog  2006-11-24 17:34:04  得分: 0  
     
     
       不是child应该是啥?  
     
      

  3.   

    我觉得,构建Child对象时会自动调用Parent(),this是当前对象,而你是在Child()内部调用Parent()那当前对象当然是Child
      

  4.   

    因为你在new Child();
    的时候 Child对象的默认构造函数会在第一句调用父类的默认构造函数
      也就是JAVA在Child对象中加入了一个
        public Child()
        {
         super();
        }
      

  5.   

    并不能new出一个父类的对象,只有一个Child类的对象,只是在产生Child对象时,先要运行父类的构造函数,然后再运行子类的构造函数。所以结果是控制台会有打印语句输出(父类构造函数的作用),而只产生一个子类对象,所以看到的引用会是子类的。
      

  6.   

    因为this代表当前的对象,你是从Child,调用的函数,所以this就是Child。跟你有多少层的父类没有任何关系。
      

  7.   

    laoliucn(想你的天空)
    并不能new出一个父类的对象,只有一个Child类的对象think in java关于继承这一章说的很清楚,在new Child()时,会产生一个父类对象,不过这个父类对象是包含在子类对象中的。
      

  8.   

    cnmahj() 说的对,this 代表是谁,new child() 就是它了
      

  9.   

    package csdn.dec;import java.awt.event.*;
    import javax.swing.*;public class MenuTest1 extends JFrame implements ActionListener
    {
    JFrame zhfr=new JFrame();
    JMenuBar mbar=new JMenuBar();
    JMenu file=new JMenu("切换");
    JMenuItem newf=new JMenuItem("添加记录");MenuTest1()
    {
    init();
    }
    public void init()
    {
    setBounds(200, 100, 700, 300);
    file.add(newf);
    mbar.add(file);
    setJMenuBar(mbar);
    //setContentPane(desktopPane);
    mtest();
    }public void mtest()
    {
    mbar.setOpaque(true);
    newf.addActionListener(this);
    }public void actionPerformed(ActionEvent e)
    {
    NewFrame();
    }public void NewFrame()
    {
    JInternalFrame jif = new JInternalFrame();
    JPanel top = new JPanel();
    JTextField tf = new JTextField();
    JButton jbt = new JButton("ddddddd");
    //jif.setContentPane(top) ;
    // jif.pack() ;
    top.add(jbt);
    top.add(tf);
    jif.add(top);
    // jif.show();
    jif.setBounds(0, 0, 200, 100);
    jif.setVisible(true);
    this.getContentPane().add(jif);
    }public static void main(String[] args)
    {
    MenuTest1 mt = new MenuTest1();
    mt.setVisible(true);
    }
    }
      

  10.   

    再想想这个
    class Parent {
      public Super() {
        g(); 
      }
      void g() {
        System.out.println("Super"); // ok,no dead loop
        //! this.g();    lead to dead loop
      }
    }class Child extends Parent {
      public static void main(String[] args) {
        new Child();
      }
      void g() {
        super.g();
      }
    }因此可以认为确实创建了超类对象吧。但这个超类对象的初始化也是编译.java时,插入到.class中的
      

  11.   

    haisenmai(我应该做得到)
    你的一大段代码没看懂,因为不懂gui编程。能不能帮我在原理上讲解一下。