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

时间:2020-08-07 11:01:29

标签: java constructor

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

我能做到吗?

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

1 个答案:

答案 0 :(得分:3)

您可以使用this关键字。

这应该可以解决问题:

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

public Rectangle(float length, float breadth) {
    //code..
}
相关问题