为什么还可以使用空白构造函数? [英] Why might one also use a blank constructor?

查看:200
本文介绍了为什么还可以使用空白构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近读一些Java,发现了一个新的东西(一个成语?):在程序中,有多个构造函数的类也将总是包含一个空的构造函数。例如:

I was reading some Java recently and came across something (an idiom?) new to me: in the program, classes with multiple constructors would also always include a blank constructor. For example:

public class Genotype {
  private boolean bits[];
  private int rating;
  private int length;
  private Random random;

  public Genotype() {              //  <= THIS is the bandit, this one right here
    random = new Random();
  }

  /* creates a Random genetoype */
  public Genotype(int length, Random r) {
    random = r;
    this.length = length;
    bits = new boolean[length];

    for(int i=0;i<length;i++) {
        bits[i] =random.nextBoolean();
    }
  }

  /* copy constructor */
  public Genotype(Genotype g,Random r) {
    random = r;
    bits = new boolean[g.length];
    rating = g.rating;
    length = g.length;

    for(int i=0;i<length;i++) {
        bits[i] = g.bits[i];
    }

  }
}

构造函数似乎不是一个真正的构造函数,似乎在每种情况下,其他的构造函数将被使用。那么为什么这个构造函数被定义?

The first constructor doesn't seem to be a "real" constructor, it seems as though in every case one of the other constructors will be used. So why is that constructor defined at all?

推荐答案

我不知道你读的代码是高质量的在过去审查了一些生物信息学代码,它不幸的是,通常不是由专业开发人员写的)。例如,第三个构造函数不是一个复制构造函数,通常在这段代码中有问题,所以我不会读得太多。

I am not sure that the code you were reading was high quality (I've reviewed some bioinformatics code in the past and it is unfortunately often not written by professional developers). For example, that third constructor is not a copy constructor and generally there are problems in this code, so I wouldn't "read too much into it".

构造函数是一个默认构造函数。它仅初始化最小值,并让用户使用getter和setter设置其余值。其他构造函数通常是方便构造函数,帮助创建具有较少调用的对象。然而,这通常会导致构造函数之间的不一致。事实上,最近的研究表明,默认构造函数与后续的setters调用是优选的。

The first constructor is a default constructor. It only initializes the bare minimum and lets users set the rest with getters and setters. Other constructors are often "convenience constructors" that help create objects with less calls. However, this can often lead to inconsistencies between constructors. In fact, there is recent research that shows that a default constructor with subsequent calls to setters is preferable.

在某些情况下,默认构造函数是关键的。例如,某些框架如digester(用于直接从XML创建对象)使用默认构造函数。 JavaBeans通常使用默认构造函数等。

There are also certain cases where a default constructor is critical. For example, certain frameworks like digester (used to create objects directly from XML) use default constructors. JavaBeans in general use default constructors, etc.

此外,一些类继承自其他类。你可能会看到一个默认的构造函数,当父对象的初始化是足够好。

Also, some classes inherit from other classes. you may see a default constructor when the initialization of the parent object is "good enough".

在这个特定的情况下,如果没有定义该构造函数,知道所有的细节提前。

In this specific case, if that constructor was not defined, one would have to know all the details in advance. That is not always preferable.

最后,一些IDE会自动生成一个默认的构造函数,所以写这个类的人恐怕会消除它。

And finally, some IDEs automatically generate a default constructor, it is possible that whoever wrote the class was afraid to eliminate it.

这篇关于为什么还可以使用空白构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆