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了~~
这样的理解对吗?
{
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了~~
这样的理解对吗?
void fun2()
{
person p = new person("hello");
p.fun1();
}
{
new person("hello");//你创建这个对象,但是没有引用变量来使用它,
this.fun1();
}