【问题标题】:how to call parameterized constructor from default constructor?如何从默认构造函数调用参数化构造函数?
【发布时间】:2020-08-07 22:51:00
【问题描述】:

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

我能做到吗?

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

【问题讨论】:

  • this(10.5f, 2.5f).
  • 还有,别忘了结束;

标签: java constructor


【解决方案1】:

您可以使用this 关键字。

这应该可以解决问题:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-05
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 2014-07-09
    相关资源
    最近更新 更多