public class Two {         int x,y;

public Two(int a, int b) {
super();
this.x = a;
this.y = b;
}
public void output(){
System.out.println(x);
System.out.println(y);
}
public void output(int x,int y){
x=x;
y=y;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Two two=new Two(3,3);
two.output(5, 5);
two.output();
}

解决方案 »

  1.   


    public   void   output(int   x,int   y){
      x=x;
      y=y;

    你这个方法写了等于没写,局部变量和成员变量同名的时候,优先选择局部变量,此时要想调用成员变量就必须用this.x this.y
      

  2.   

    原因很简单:public   void   output(int   x,int   y){ 
    x=x; 
    y=y; 
    } 在你调用
    two.output(5,   5); 
    时候是把你的函数中的局部变量自己给了自己,该类的属性没有任何影响。
    修改为:public   void   output(int   x,int   y){ 
    this.x=x; 
    this.y=y; 

      

  3.   

    楼上的,要是这样改的话,two.output(); 方法输出的也是5,5了
    lz 首先你output(int   x,int   y)方法少了打印语句,即使调用了也不会做任何处理,另外就是this的问题
    this指的是当前对象,你用two调用output方法 this指的就是two这个对象
    如果你想得到3355的话,就按楼上说得之后把two.output(5,   5); 
    two.output(); 这两个方法调换个位置
      

  4.   

    8楼正解,x=x和y=y等于把自己赋给自己,需要使用this才能调用类中的成员
      

  5.   

    public   class   Two   {                 int   x,y; public   Two(int   a,   int   b)   { 
    super(); 
    x=a; 
    y=b; 

    public   void   output(){ 
    System.out.println(x); 
    System.out.println(y); 

    public   void   output(int   x,int   y){ 
    this.x=x; 
    this.y=y; 

    public   static   void   main(String[]   args)   { 
    //   TODO   Auto-generated   method   stub 
    Two   two=new   Two(3,3); 
    two.output(5,   5); 
    two.output(); 
    }
    如果你想要输出5,5的话,程序改成这样吧!
      

  6.   

    public class Two{ 
    int x,y; 
    public Two(int a,int b){ 
    super();        //super()没意义,可以删掉,因为Two是继承Object类的,它会自动调用Object的构造器
    this.x = a; 
    this.y = b; 

    public void output(){ 
    System.out.println(x); 
    System.out.println(y); 

    public void output(int x,int y){ 
    x=x;          //应该是this.x=x
    y=y;          //应该是this.y=y

    public static void main(String[] args){
    Two two = new Two(3,3); 
    two.output(5,5); 
    two.output();
    }
    }
      

  7.   

    public class Two{ 
    int x,y; 
    public Two(int a,int b){ 
    super();        //super()没意义,可以删掉,因为Two是继承Object类的,它会自动调用Object的构造器
    this.x = a; 
    this.y = b; 

    public void output(){ 
    System.out.println(x); 
    System.out.println(y); 

    public void output(int x,int y){ 
    x=x;          //应该是this.x=x
    y=y;          //应该是this.y=y

    public static void main(String[] args){
    Two two = new Two(3,3); 
    two.output(5,5); 
    two.output();
    }
    }