构造函数中的 If 语句

If statements inside of constructors

我有一个用空构造函数调用的 class,如果构造函数已用于 [=18] 的实例化,我希望有一个将布尔值设置为 true 的 if 语句=].例如:

public class Test {
public float a1;
public int b1;
public double c1;
public Boolean ifConsUsed = false;

public Test(float a, int b, double c, Main app) {
   a1 = a; b1 = b; c1 = c; applet = app;
      if(expression goes here) {
      ifConsUsed = true;
      }
   }

}

在这种情况下,我会在 if 语句中放入什么表达式来检测是否使用了构造函数?

如果您的目的只是想弄清楚是否调用构造函数,则不需要设置任何条件ifConsUsed

 public Test(float a, int b, double c, Main app) {
      ifConsUsed = true;
       ....
         }

如果构造函数运行了,它本身就意味着一个对象已经被实例化了。 一个class的构造函数被调用它本身意味着对象已经被创建。所以,保持简单..

public class Test 
{
    public float a1;
    public int b1;
    public double c1;
    public Boolean ifConsUsed = false;

    public Test(float a, int b, double c, Main app)
   {
        a1 = a; b1 = b; c1 = c; applet = app;
        ifConsUsed = true;
   }
}