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

Object s=this之后s为什么不能代替this使用(有示例)?
public   class   aa
{
int   num=100;
}
class   bb   extends   aa
{
int   num=200;
void   qq()
{
Object   s=this;
System.out.println(s.num);//引处为什么把s换成this就可以了?s为什么不可以啊?

}
public   static   void   main(String   args[])
{
System.out.println( "hello,world ");
bb   t=new   bb();
t.qq();
}
}

------解决方案--------------------
不能把this赋给别人吧
------解决方案--------------------
改成 bb s=this;就可以了
因为 Object s=this;这样时,s在编译器看来只是一个Object对象而不是bb,所以它不存在num属性
------解决方案--------------------
没有把this附值给变量的,去看看this用法
http://publish.it168.com/2005/0730/20050730028901.shtml

------解决方案--------------------
System.out.println(((bb)s).num);
System.out.println(((aa)s).num);
------解决方案--------------------
同意楼上的
如果不转换类型,就无法调用其变量
------解决方案--------------------
this表示当前对象的应用
不能给this赋值
------解决方案--------------------
同意malligator