简体   繁体   English

构造函数和关键字“ this”

[英]Constructors and Keyword 'this'

I have a syntax error and I don't Know how to fix it. 我有语法错误,但不知道如何解决。 The code appears correct to me, but Eclipse is telling me that "Constructor call must be the first statement in a constructor" at the methods setName() and setAge() 该代码对我来说似乎是正确的,但是Eclipse在setName()setAge()方法中告诉我“构造函数调用必须是构造函数中的第一条语句”

 public class KeywordThis {


    private String name;
    private int age;

    public KeywordThis(){

        this.name = "NULL";
        this.age = 0;

    }

    public KeywordThis(String s, int a){

        this.name = s;
        this.age = a;

    }

    public KeywordThis(String s){

        this.name = s;      
    }

    public KeywordThis(int a){

        this.age = a;

    }

    public int setAge(int a){

        this(a);
    }

    public String setName(String s){

        this(s);
    }






    public static void main(String args[] ){








    }


}

You cannot call a constructor like that from an instance method. 您不能从实例方法中调用类似的构造函数。 You want your setter to change the value of the object you already have, not create a new one. 您希望设置器更改现有对象的值,而不是创建新对象。 I think you mean to do this: 我认为您的意思是这样做:

public void setAge(int a){

    this.age = a;
}

public void setName(String s){

    this.name = s;
}

Also note that your setters don't usually return values, so I've changed them to return type void. 还要注意,您的设置器通常不返回值,因此我将它们更改为返回void类型。

Once an object has been created you can not manually call the constructor. 创建对象后,您将无法手动调用构造函数。 Constructors can only be called inside another constructor. 构造函数只能在另一个构造函数内部调用。

As others have pointed out it should be: 正如其他人指出的那样,应该是:

public void setAge(int a) {
    this.a = a;
}

As a note, your setters should look like 请注意,您的二传手应该看起来像

public void setAge(a) {
   this.a = a;
}

and not construct a new object. 而不构造一个新对象。 If you don't do this, you are breaking an ubiqutous Java convention. 如果您不这样做,那么您将违反无处不在的Java约定。

Assuming you want to create a new instance in a setter, you would do something like 假设您想在设置器中创建一个新实例,您将执行以下操作

public KeywordThis setAge(a){
   return new KeywordThis(a);
}

and not use this(a) . 而不使用this(a) Using this as you are attempting should only be done in a constructor (to invoke another constructor for the same class). 尝试使用this ,只能在构造函数中完成(为同一类调用另一个构造函数)。

public class KeywordThis { 公共类关键字This {

private String name;
private int age;

public KeywordThis(){

    this.name = "NULL";
    this.age = 0;

}

public KeywordThis(String s, int a){

    this.name = s;
    this.age = a;

}

public KeywordThis(String s){

    this.name = s;      
}

public KeywordThis(int a){

    this.age = a;

}

public int setAge(int a){

    this(a);
    int b=a;
    return b;
}

public String setName(String s){

    this(s);
    String s1=s;
    return s; 
}






public static void main(String args[] ){

   KeywordThis ob1=new Keyword();
   ob1.setAge(20);
   ob1.setName("sandy");

}


}

java share|edit Java共享|编辑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM