实例变量声明和实例化 [英] Instance Variable Declaration and instantiation

查看:129
本文介绍了实例变量声明和实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这两种实例化实例变量的方法有什么区别。在任何一种情况下,当创建 PersonDirectory 的实例时,它将创建的实例。

I want to know what is the difference between these two ways of instantiating instance variable. In either case it would create an instance of the persons when an instance of PersonDirectory is created.

public class PersonDirectory {
    private ArrayList<Person> persons = new ArrayList<>();

}


public class PersonDirectory {
    private ArrayList<Person> persons;
    public PersonDirectory(){
        persons = new ArrayList<Person>();
    }
}


推荐答案

他们非常相似,对于基本编程,它们可以被认为是等价的。您可能会注意到的最明显的事情是当您向 PersonDirectory 添加另一个构造函数时,如下所示:

They are so similar that for basic programming they could be considered equivalent. The most obvious thing you might notice is when you add another constructor to PersonDirectory, like so:

public class PersonDirectory {
    private ArrayList<Person> persons;
    private DirectoryAdmin admin;
    public PersonDirectory() {
        persons = new ArrayList<Person>();
    }
    public PersonDirectory(DirectoryAdmin initialAdmin) {
        admin = initialAdmin;
    }
}

如果您使用第二个构造函数,您会发现构造PersonDirectory后,人员为空。这是因为Java不会自动为您运行其他构造函数。您可以通过添加对 this()的显式调用来解决问题,该调用还运行与 call。

If you utilize this second constructor, you would find that persons is null after constructing PersonDirectory. This is because Java does not run other constructors for you automatically. You could fix the problem by adding an explicit call to this(), which runs also the constructor that matches the signature of the this call.

public class PersonDirectory {
    private ArrayList<Person> persons;
    private DirectoryAdmin admin;
    public PersonDirectory() {
        persons = new ArrayList<Person>();
    }
    public PersonDirectory(DirectoryAdmin initialAdmin) {
        this();
        admin = initialAdmin;
    }
}

但程序员经常会忘记添加对此的调用();并且可能发现太晚了,因为他们的一个建造者是不小心写的,所以人们被遗弃了。

But often times programmers forget to add the call to this(); and may find out too late that persons has been left null because one of their constructors was written carelessly.

如果您改为使用声明内联编写初始化,则无论您调用哪个PersonDirectory构造函数,都会运行初始化,因此可以认为它不易出错。 / p>

If you instead write the initialization inline with the declaration, the initialization is run regardless of which PersonDirectory constructor you call, and so could be considered slightly less error-prone.

public class PersonDirectory {
    private ArrayList<Person> persons = new ArrayList<Person>();
    private DirectoryAdmin admin;
    public PersonDirectory() {
    }
    public PersonDirectory(DirectoryAdmin initialAdmin) {
        // don't have to worry about forgetting to call this();
        admin = initialAdmin;
    }
}

然而,有些理由有时候更喜欢初始化构造函数。例如,它可以为子类及其构造函数提供更多控制。

There are, however, reasons to sometimes prefer initialization in the constructor. For example, it can give more control to subclasses and their constructors.

声明成员变量 final 尽可能。这样编译器就会提醒你是否编写了一个构造函数,使一些字段保持未初始化状态。

It is a good practice to declare your member variables final whenever possible. This way the compiler can remind you if you've written a constructor that leaves some fields uninitialized.

内联初始化语句总是在类的构造函数之前运行。

Inline initialization statements always run before the class's constructor(s).

这篇关于实例变量声明和实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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