Merriam Webster describes invoke
like this:
:提(某人或某事),企图使人们感到某种方式或在他们的心中有一定的想法
:在支持指(东西)你的想法
:利用(法律,右等)的
所以,是的,我会说你可以调用一个反面结构很像你可以调用一个方法。但随着the java specifcation说
构造函数永远不会被方法调用表达式调用
这样的构造只能通过创建对象即可调用:
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class InvokeConstructor {
public InvokeConstructor() {
System.out.println("constructor1");
}
public InvokeConstructor(final boolean unused) {
System.out.println("constructor2");
}
public static void main(String[] args) {
new InvokeConstructor();
new InvokeConstructor(false);
// And using reflection:
try {
Constructor<InvokeConstructor> constructor1 = InvokeConstructor.class.getConstructor();
constructor1.newInstance();
Constructor<InvokeConstructor> constructor2 = InvokeConstructor.class.getConstructor(Boolean.TYPE);
constructor2.newInstance(Boolean.FALSE);
} catch (NoSuchMethodException e) {
e.printStackTrace(); // TODO: implement catch
} catch (InvocationTargetException e) {
e.printStackTrace(); // TODO: implement catch
} catch (InstantiationException e) {
e.printStackTrace(); // TODO: implement catch
} catch (IllegalAccessException e) {
e.printStackTrace(); // TODO: implement catch
}
}
}
这只是争论的语义。但是FWIW,JLS会谈到“调用构造函数”(例如http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.8)。 – 2014-11-01 21:58:03
您不像普通方法那样调用构造函数,但是运算符'new'调用它。 – 5gon12eder 2014-11-01 22:02:14