java - 为什么有些带参数的构造函数仍然调用this()?好像什么也没做?

标签 java constructor linked-list

以下代码来自1.8版本的LinkedList.java。 我不明白为什么调用 this() ?构造函数 LinkedList() 似乎什么也没做?

/**
 * Constructs an empty list.
 */
public LinkedList() {
}

/**
 * Constructs a list containing the elements of the specified
 * collection, in the order they are returned by the collection's
 * iterator.
 *
 * @param  c the collection whose elements are to be placed into this list
 * @throws NullPointerException if the specified collection is null
 */
public LinkedList(Collection<? extends E> c) {
    this();
    addAll(c);
}

最佳答案

你是对的。它什么也不做。空构造函数调用super()默认情况下。没有this()第二个构造函数也会调用super()默认情况下。我的猜测是 LinkedList() 中曾经进行过一些初始化并调用this() ,此初始化也用于 LinkedList(Collection<? extends E> c) 。现在它是无用且无害的剩余物。

关于java - 为什么有些带参数的构造函数仍然调用this()?好像什么也没做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38478876/

相关文章:

java - 多个连接包同时到达的Netty CorruptedFrameException

c# - 返回从未使用过的对象的构造函数是否浪费?

c++ - C++中的链表数组

c++ - 需要链表解释

java - Jboss类冲突

java - 来自其他类的 JFrame/JPanel 绘图

java - 无法从资源目录加载属性文件

python - __new__() 在 python 2.7.12 中不返回类实例时的继承

c++ - 如何从具有不同构造函数的父类(super class)访问子类的成员?

java - java中如何从链表中找到中间值或节点?