Java 如何从默认构造函数调用参数化构造函数?

Java 如何从默认构造函数调用参数化构造函数?,java,constructor,Java,Constructor,我想从公共java类中的默认构造函数调用参数化构造函数 我能做到吗 public Rectangle() { Rectangle(10.5f,2.5f) //this not working } public Rectangle(float length, float breadth) { code... } 您可以使用this关键字 这应该可以做到: public Rectangle() { this(10.5f, 2.5f); } public Rectangle(flo

我想从公共java类中的默认构造函数调用参数化构造函数

我能做到吗

public Rectangle()
{
Rectangle(10.5f,2.5f)     //this not working
}
public Rectangle(float length, float breadth)
{
code...
}

您可以使用
this
关键字

这应该可以做到:

public Rectangle() {
    this(10.5f, 2.5f);
}

public Rectangle(float length, float breadth) {
    //code..
}

您可以使用
this
关键字

这应该可以做到:

public Rectangle() {
    this(10.5f, 2.5f);
}

public Rectangle(float length, float breadth) {
    //code..
}

这个(10.5f,2.5f)
。另外,不要忘记关闭<代码>这个(10.5f,2.5f)。另外,不要忘记关闭;