在其他指令之后从构造函数调用另一个构造函数
Call another constructor from constructor after other instructions
我有 2 个构造函数,接受不同类型的参数:
public Board(String s) {
// the string is parsed to an int array.
int[] array = doSomething(s);
this(array);
}
public Board(int[] array) {
doSomethingElse(s);
}
但是在第一个构造函数中我得到 "Constructor call must be the first statement in a constructor"。有没有办法让构造函数在执行其他操作后调用另一个构造函数,或者它只是 Java?
的限制
它不一定是构造函数。你可以这样做:
public Board(String s) {
// the string is parsed to an int array.
int[] array = doSomething(s);
this.sharedMethod(array);
}
public Board(int[] array) {
this.sharedMethod(array);
}
public void sharedMethod() {
//yourLogic
}
根据Java
的规则,this
应该首先被调用:
public Board(String s) {
this(doSomething(s));
}
不,您不能在另一个构造函数中执行其他操作后调用构造函数。构造函数是Java中非常特殊的方法。但是你有两个选择:
1。如果你在调用另一个构造函数之前要做的只是对参数进行预处理,你可以这样写:
public Board(String s) {
this(doSomething(s));
}
private static int[] doSomething(String s) {...}
您可以调用任何静态方法并将其结果传递给另一个构造函数。
2。如果您的预处理意味着修改当前对象,那么您不能使用静态方法执行此操作,您可以调用特殊方法(如来自两个构造函数的 init()
):
public Board(String s) {
int[] array = doSomething(s);
init(array);
}
public Board(int[] array) {
init(array);
}
private void init(int[] array) {
// do something else
}
是 java 当您从另一个构造函数调用构造函数时,构造函数调用应该是第一个语句。查看以下代码片段和注释 -
public class Board{
//no-argument consturctor
public Board(){
//if the 'Board' class has a super class
//then the super class constructor is called from here
//and it will be the firs statement
//the call can be explicit or implicit
//Now do something
}
public Board(int size){
//call to no-arg constructor
this(); // explicit call to the no-argument constructor - 'Board()'
//Now you can do something
//but after doing something you can not
//make an explicit/implicit constructor call.
}
}
我有 2 个构造函数,接受不同类型的参数:
public Board(String s) {
// the string is parsed to an int array.
int[] array = doSomething(s);
this(array);
}
public Board(int[] array) {
doSomethingElse(s);
}
但是在第一个构造函数中我得到 "Constructor call must be the first statement in a constructor"。有没有办法让构造函数在执行其他操作后调用另一个构造函数,或者它只是 Java?
的限制它不一定是构造函数。你可以这样做:
public Board(String s) {
// the string is parsed to an int array.
int[] array = doSomething(s);
this.sharedMethod(array);
}
public Board(int[] array) {
this.sharedMethod(array);
}
public void sharedMethod() {
//yourLogic
}
根据Java
的规则,this
应该首先被调用:
public Board(String s) {
this(doSomething(s));
}
不,您不能在另一个构造函数中执行其他操作后调用构造函数。构造函数是Java中非常特殊的方法。但是你有两个选择:
1。如果你在调用另一个构造函数之前要做的只是对参数进行预处理,你可以这样写:
public Board(String s) {
this(doSomething(s));
}
private static int[] doSomething(String s) {...}
您可以调用任何静态方法并将其结果传递给另一个构造函数。
2。如果您的预处理意味着修改当前对象,那么您不能使用静态方法执行此操作,您可以调用特殊方法(如来自两个构造函数的 init()
):
public Board(String s) {
int[] array = doSomething(s);
init(array);
}
public Board(int[] array) {
init(array);
}
private void init(int[] array) {
// do something else
}
是 java 当您从另一个构造函数调用构造函数时,构造函数调用应该是第一个语句。查看以下代码片段和注释 -
public class Board{
//no-argument consturctor
public Board(){
//if the 'Board' class has a super class
//then the super class constructor is called from here
//and it will be the firs statement
//the call can be explicit or implicit
//Now do something
}
public Board(int size){
//call to no-arg constructor
this(); // explicit call to the no-argument constructor - 'Board()'
//Now you can do something
//but after doing something you can not
//make an explicit/implicit constructor call.
}
}