简体   繁体   English

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

[英]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.

相关问题 如何从另一个类中的public类型的参数化构造函数中调用默认类型的参数化构造函数? - How can I call parameterized constructor of default type from a parameterized constructor of type public which is in another class? 从参数化构造函数调用默认构造函数的任何方法? - Any way to call the default constructor from a parameterized constructor? 我可以在java中的公共类中调用参数化构造函数中的默认构造函数吗? - Can I call default constructor from parameterized constructor inside public class in java? 如何从java中的另一个类调用参数化构造函数 - How to call parameterized constructor from another class in java 调用类的参数化/默认构造函数? - Invoking parameterized/default constructor of a class? 如何从参数化构造函数获取数据? - How to get data from a parameterized constructor? 如何从默认构造函数中调用set方法? - How do you call a set method from a default constructor? 从默认构造函数调用参数构造函数,而不使用此关键字 - Call parameter constructor from default constructor without using this keyword 如何使用默认访问修饰符创建构造函数是参数化构造函数的类的对象 - How to create the object of class where constructor is parameterized constructor using default access modifier 如何在参数化构造函数中使用枚举? - How to use enum in a parameterized constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM