当前位置: 代码迷 >> Java相关 >> [求助]this关键字问题
  详细解决方案

[求助]this关键字问题

热度:78   发布时间:2007-04-24 13:31:58.0
[求助]this关键字问题
public class Group
{
private static int count;
private String name;
public class Student
{
private int count;
private String name;
public void output(int count)
{
count++;
this.count++;
Group.count++;
Group.this.count++;//谁能解释一下这条语句--这里的this指的是哪个对象(实例)?
System.out.println(count+" "+this.count+" "+Group.count+" "+Group.this.count);
}
}
public Student aStu()
{
return new Student();
}
public static void main(String [] args)
{
Group g=new Group();
g.count=10;
Group.Student s=g.aStu();
s.output(5);
}
}

Group.this.count++;//谁能解释一下这条语句--这里的this指的是哪个对象(实例)?

Group.count++; //这两句有什么区别? 谢谢
Group.this.count++;

搜索更多相关的解决方案: 字问题  关键  

----------------解决方案--------------------------------------------------------
是student 类

----------------解决方案--------------------------------------------------------

你的程序要报错吧,在一个类里只能写一个public class类,其它类直接写class,不用写public,
关于this这关键字很简单,你在哪个类写的this,这个就是属于哪个类的this,super就是父类


----------------解决方案--------------------------------------------------------
public void output(int count)
{
count++;//此方法中的count
this.count++;//此类中的count
Group.count++; //group中的count
Group.this.count++;//谁能解释一下这条语句--这里的this指的是哪个对象(实例)?跟上面一样
System.out.println(count+" "+this.count+" "+Group.count+" "+Group.this.count);
}
----------------解决方案--------------------------------------------------------
从运行结果看,应该指的是Group里的count.Group.count和Group.this.count是一样的。因为输出结果是:6 1 12 12.关键是你把Group里的count搞成了静态的,所以可以用Group.count.但是如果不是静态的话,就不能用Group.count来访问了。

[此贴子已经被作者于2007-4-24 21:48:00编辑过]



----------------解决方案--------------------------------------------------------
  相关解决方案