/ **
* inner class to access external static variable
* @ author Administrator
*
* /
public class GroupThree {
private static int count;
private String name;
public class Student {
private int count;
private String name;
public void output (int count) {
count + +;
this.count + +;
GroupThree.count + +;
GroupThree.this.count + +;
System.out.println (count + "" + this.count + ""
+ GroupThree.count + "" + GroupThree.this.count);
}
}
public Student aStu () {
return new Student ();
}
public static void main (String [] args) {
GroupThree g3 = new GroupThree ();
GroupThree.count = 10;
GroupThree.Student s1 = g3.aStu ();
s1.output (5);
}
}
The output is 611,212
two solutions given above no doubt, but given later 12 , what is the reason that way ? ?
------ Solution ---------------------------------------- ----
reason is very simple:
GroupThree.count + +;
GroupThree.this.count + +;
two lines of code in the actual count are private static int count;
I think you do not understand something here GroupThree.this, you think this is a Student object , right ? Actually not, it is GroupThree using the new object created .
------ Solution ---------------------------------------- ----
GroupThree.this.count in the past based on the increase again.
------ Solution ---------------------------------------- ----
public void output(int count){
count++;
this.count++;
GroupThree.count++;
System.out.println(count+" "+this.count+" "
+GroupThree.count+" "+GroupThree.this.count);
GroupThree.this.count++;
System.out.println(count+" "+this.count+" "
+GroupThree.count+" "+GroupThree.this.count);
}
added to it , then run to see the results you know why not 11 up ! Because + + twice ! Then the output !
------ For reference only -------------------------------------- -
I know , but why it is 12 instead of 11 ?
------ For reference only -------------------------------------- -
I know , but why it is 12 instead of 11 ?
landlord, you want me to say ?
Since GroupThree.count + +; and GroupThree.this.count + +; is pointing to the same object, then twice + + operator adds two to think in pictures. You assign a value to 10,10 +2 = ? .
没有评论:
发表评论