读书人

this为什么不可以这样传递?该如何解决

发布时间: 2012-01-01 23:10:55 作者: rapoo

this为什么不可以这样传递?
class hu
{
private int age;
void fun()
{
System.out.println( "hufang age is "+age);
}
void fun1(hu a)
{
a.fun();

}


public static void main(String [] args)
{
hu hudawei =new hu();
hu huna=new hu();
hudawei.fun1(this);
}
}
this当成调用fun1的对象传递有错吗?为什么会出错啊?

[解决办法]
this不能用在static方法里。

因为那个时候this还没有出生呢

读书人网 >J2SE开发

热点推荐