class person
{   
    String name;
    person(){}
    person(String name)
    {
       this.name=name;
    }
    void fun1()
    {
       System.out.println(name);
    }
    void fun2()
    {    
       new person("hello");   
       this.fun1(); 
    }
}
class gcx
{
public static void main(String[] args)
{
    person a=new person();
    a.fun2();
}
}
结果显示的是null~按我理解的,应该能显示出hello~~
我理解的流程是:先在main里创建对象,并调用fun2(),在fun2()中先执行person有参数的构造函数~这时name等于hello,this.name也是hello,this.name代表person类开始时声明的name~~再执行fun1(),this.fun1()的意思就是调用fun2()的对象也调用fun1(),在System.out.println中的name是person类开始时声明的name~~这样就输出hello了~~
这样的理解对吗?

解决方案 »

  1.   

    你没有对a中的对像赋值,它的输出能不为空?this.fun1()= a.fun2()   a中的name 没有
      

  2.   

    你定义了两个对象,在主方法中调用的是第一个,即为person(){}.里面什么都没有的.
      

  3.   

    this.fun1(); 此时的this是a对象,所以调用的是a对象的func1(),而a对象的name属性为null,所以打印出来就为null。如果要打印出来的话,fun2()可以修改成
        void fun2()
        {   
           person p = new person("hello");  
           p.fun1(); 
        }
      

  4.   

    void fun2()
        {   
           new person("hello");//你创建这个对象,但是没有引用变量来使用它,  
           this.fun1(); 
        }
      

  5.   

    this是“自己”的意思,delphi中对应的关键字是self,我觉得更贴切。