[英]how to call parameterized constructor from default constructor?
I want to call parameterized constructor from default constructor inside a public java class.我想从公共 java class 中的默认构造函数调用参数化构造函数。
Can I achieve it?我能实现吗?
public Rectangle()
{
Rectangle(10.5f,2.5f) //this not working
}
public Rectangle(float length, float breadth)
{
code...
}
You can use the this
keyword.您可以使用
this
关键字。
This should do the trick:这应该可以解决问题:
public Rectangle() {
this(10.5f, 2.5f);
}
public Rectangle(float length, float breadth) {
//code..
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.