class A{
int x,y;
class B{
     void  setValues(int x,int y){ this.x=x;this.y=y;}
}
void print(){
B b=new B();
  b.setValues(2,3);
  System.out.println(x+y);
}
}为什么说找不到符号x,y。。

解决方案 »

  1.   


    class B{
      void setValues(int x,int y){ this.x=x;this.y=y;}
    }
    这里面得this指针指向的是B的对象,而非A对象,B对象没有x,y属性,因此this.x,this.y当然不存在。谢谢
      

  2.   

    void setValues(int x,int y){ A.this.x=x;A.this.y=y;} //注明是哪个类的this
      

  3.   

    连带着给你测试了
    public class DDD
    {
    public static void main(String[] args)
    {
    A a = new A();
    a.print();
    }
    }class A
    {
    int x, y; class B
    {
    void setValues(int x, int y)
    {
    A.this.x = x;
    A.this.y = y;
    }
    } void print()
    {
    B b = new B();
    b.setValues(2, 3);
    System.out.println(x + y);
    }
    }