是否可以从另一个(在同一个类中,而不是从子类中)调用构造函数?如果有,怎么样?什么是调用另一个构造函数的最佳方法(如果有几种方法可以做到)?
是否可以从另一个(在同一个类中,而不是从子类中)调用构造函数?如果有,怎么样?什么是调用另一个构造函数的最佳方法(如果有几种方法可以做到)?
对的,这是可能的:
public class Foo {
private int x;
public Foo() {
this(1);
}
public Foo(int x) {
this.x = x;
}
}
要链接到特定的超类构造函数而不是同一个类中的一个,请使用 super
代替 this
。注意 你只能链接到一个构造函数,和 它必须是构造函数体中的第一个语句。
也可以看看 这个相关的问题,这是关于C#,但适用相同的原则。
运用 this(args)
。首选模式是从最小的构造函数到最大的构造函数。
public class Cons {
public Cons() {
// A no arguments constructor that sends default values to the largest
this(madeUpArg1Value,madeUpArg2Value,madeUpArg3Value);
}
public Cons(int arg1, int arg2) {
// An example of a partial constructor that uses the passed in arguments
// and sends a hidden default value to the largest
this(arg1,arg2, madeUpArg3Value);
}
// Largest constructor that does the work
public Cons(int arg1, int arg2, int arg3) {
this.arg1 = arg1;
this.arg2 = arg2;
this.arg3 = arg3;
}
}
您还可以使用最近提倡的valueOf或仅仅是“of”的方法:
public class Cons {
public static Cons newCons(int arg1,...) {
// This function is commonly called valueOf, like Integer.valueOf(..)
// More recently called "of", like EnumSet.of(..)
Cons c = new Cons(...);
c.setArg1(....);
return c;
}
}
要调用超类,请使用 super(asdf)
。对super的调用必须是构造函数中的第一个调用,否则您将收到编译器错误。
[注意:我只想添加一个方面,我在其他答案中没有看到:如何克服这个()必须在第一行的要求的限制。]
在Java中,可以从构造函数中调用同一类的另一个构造函数 this()
。但请注意 this
必须在第一线。
public class MyClass {
public MyClass(double argument1, double argument2) {
this(argument1, argument2, 0.0);
}
public MyClass(double argument1, double argument2, double argument3) {
this.argument1 = argument1;
this.argument2 = argument2;
this.argument3 = argument3;
}
}
那 this
必须出现在第一行看起来像一个很大的限制,但你可以通过静态方法构造其他构造函数的参数。例如:
public class MyClass {
public MyClass(double argument1, double argument2) {
this(argument1, argument2, getDefaultArg3(argument1, argument2));
}
public MyClass(double argument1, double argument2, double argument3) {
this.argument1 = argument1;
this.argument2 = argument2;
this.argument3 = argument3;
}
private static double getDefaultArg3(double argument1, double argument2) {
double argument3 = 0;
// Calculate argument3 here if you like.
return argument3;
}
}
当我需要从代码内部调用另一个构造函数(而不是在第一行)时,我通常使用这样的辅助方法:
class MyClass {
int field;
MyClass() {
init(0);
}
MyClass(int value) {
if (value<0) {
init(0);
}
else {
init(value);
}
}
void init(int x) {
field = x;
}
}
但是大多数情况下,我试图通过从第一行中较简单的构造函数调用更复杂的构造函数来反过来做到这一点。对于上面的例子
class MyClass {
int field;
MyClass(int value) {
if (value<0)
field = 0;
else
field = value;
}
MyClass() {
this(0);
}
}
在构造函数中,您可以使用 this
用于在同一个类中调用另一个构造函数的关键字。这样做被称为 显式构造函数调用。
这是另一个Rectangle类,其实现与Objects部分中的实现不同。
public class Rectangle {
private int x, y;
private int width, height;
public Rectangle() {
this(1, 1);
}
public Rectangle(int width, int height) {
this( 0,0,width, height);
}
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}
该类包含一组构造函数。每个构造函数初始化一些或所有矩形的成员变量。
正如大家已经说过的那样,你使用了 this(…)
,这被称为 显式构造函数调用。
但是,请记住这一点 在这样一个显式的构造函数调用语句中 你可能不会参考
this
要么 super
。如JLS(§8.8.7.1)中所述。
我会告诉你一个简单的方法
有 二 构造函数的类型:
我将在一个例子中解释
class ConstructorDemo
{
ConstructorDemo()//Default Constructor
{
System.out.println("D.constructor ");
}
ConstructorDemo(int k)//Parameterized constructor
{
this();//-------------(1)
System.out.println("P.Constructor ="+k);
}
public static void main(String[] args)
{
//this(); error because "must be first statement in constructor
new ConstructorDemo();//-------(2)
ConstructorDemo g=new ConstructorDemo(3);---(3)
}
}
在上面的例子中,我展示了3种类型的呼叫
注意: 这必须是构造函数中的第一个语句。
您可以使用“this”关键字从同一个类的另一个构造函数构造一个构造函数。 示例 -
class This1
{
This1()
{
this("Hello");
System.out.println("Default constructor..");
}
This1(int a)
{
this();
System.out.println("int as arg constructor..");
}
This1(String s)
{
System.out.println("string as arg constructor..");
}
public static void main(String args[])
{
new This1(100);
}
}
输出 - 字符串作为arg构造函数.. 默认构造函数.. int作为arg构造函数..