为什么我会从另一个(Java)调用构造函数?

时间:2016-07-20 14:01:45

标签: java

刚刚开始学习java,在我的教科书中,我遇到了this,这一开始非常混乱,但现在开始有意义了。现在,在我的书中,我们 开始构造函数的基本应用程序,并在页面上注明this,也可用于调用其他构造函数。那时候我有点困惑 关于this关于SO的其他问题。我想我现在已经达到了一定程度,但为什么我会想要这样做呢?考虑一下我刚刚编写的以下内容。

private double balance;
private double interest;
public Account(double initialBalance){
    balance = initialBalance;

}
public Account(double balance, double interest){
    this(0);
    balance = initialBalance;
    this.interest = interest;
}

此处this(0);,根据我的理解,查找具有一个参数的另一个构造函数,查找Account(double initialBalance),并将initialBalance设置为零。 大。嗯,但为什么我不能直接这样做呢?只需将余额设为零即可!我相信它非常有用,但我无法想到任何例子。 谢谢!

8 个答案:

答案 0 :(得分:5)

非常实用且避免代码重复

public Account(){
    this(0);
}

public Account(double initialBalance){
    this(initialBalance, DEFAULT_INTEREST_RATE);
}

public Account(double balance, double interest){
    balance = initialBalance;
    this.interest = interest;
}

less 参数的构造函数使用 more 参数委托给构造函数,为缺少的参数传递默认值。

如果这不可能,则需要一个接受参数的人工init(..)方法,并从所有构造函数调用。这样不太安全,因为可以重复调用此方法。

答案 1 :(得分:3)

来自Java documentation的示例可能比您手中的示例更有意义:

public class Rectangle {
    private int x, y;
    private int width, height;

    public Rectangle() {
        this(0, 0, 1, 1);
    }
    public Rectangle(int width, int height) {
        this(0, 0, width, height);
    }
    public Rectangle(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
    ...
}

答案 2 :(得分:1)

想想你可以用另一种方式做到这一点:你的基本"构造函数是

public Account(double balance, double interest){
    balance = initialBalance;
    this.interest = interest;
}

并基于此,您可以添加一些简化:

public Account(double initialBalance){
    this(initialBalance, DEFAULT_INTEREST);
}

public Account(){
    this(DEFAULT_INITIAL_BALANCE, DEFAULT_INTEREST);
}

调用这些构造函数可以简化主要行为的改变:如果我,e。例如,想要在某处注册新对象,我可以在一个中心位置注册,让其他构造函数依赖它。

答案 3 :(得分:1)

这是使用同一个Object的另一个构造函数的一个很好的例子:

public Person(String name, String street, String location) {
   (...)
   //handle them
}

public Person() {
   this("default name", "default street", "default location");
}

它基本上是一个快捷方式(重载),因此您可以避免冗余代码。

答案 4 :(得分:1)

这个例子确实没有多大意义。

以下是。

private double balance;
private double interest;

public Account(double initialBalance){
    this(initialBalance, 9.99);
}

public Account(double balance, double interest){
    this.balance = balance;
    this.interest = interest;
}

确实有人调用另一个构造函数,这通常会做一些工作,而不仅仅是分配。

对于原始示例,可能是首先创建了简单的构造函数,然后为额外的字段interest添加了带有额外参数的构造函数。

因此,人们可能会经常看到此构造,并且与super(...)

的调用相当

同样在简单的情况下,这种用法遵循DRY原则:不要重复自己。请注意,如果其中一个构造函数在时间上只有一些不同的赋值,那么程序将依赖于构造函数。现在您知道,相同的代码已经完成,您不必再测试该功能N次。

答案 5 :(得分:1)

重载构造函数可能会多次派上用场。 一个例子是当你有多个参数但不是所有参数都是强制性的。

使用重载可以执行以下操作:

public Account(string id, double balance, string name, strings address){
    this.id = id;
    this.balance = balance;
    this.name = name;
    this.address = address;
}

public Account(string id, double balance, string name){
    this(id, balance, name, null);
}

public Account(string id, double balance){
    this(id, balance, "Unknown" ,null);
}

public Account(string id){
    this(id, 0, "Unknown" ,null);
}

答案 6 :(得分:0)

这称为"构造函数重载"。就像方法重载一样,这是Java允许您为单个方法提供不同数量的参数的方法。

它通常用于方法,或者在这种情况下,是具有"可选"的构造函数。参数。下面是一个应该让它更明显的例子:

class Cat{
    private int paws;
    private String name;

    public Cat(String name){
        //Assume that the cat is physically not handicapped, and thus has 4 paws
        this(name,4);
    }

    public Cat(String name, int paws){
        this.name = name;
        this.paws = paws;
    }
}

答案 7 :(得分:0)

如果你反过来说它会更有意义。

public Account(double initialBalance){
    this(initialBalance, 2.0); // 2.0 being a default interest (whatever you'd like, could be 0).
}

public Account(double balance, double interest){
    this.balance = balance;
    this.interest = interest;
    // Some more very difficult business logic
}

这样可以防止重复代码。难以改变的业务逻辑只需要改变一次(如果需要)。

相关问题