LinkedList数据结构,为什么我们需要从其他构造函数调用空的默认构造函数 [英] LinkedList data structure, why we need to call empty default constructor from a different constructor

查看:97
本文介绍了LinkedList数据结构,为什么我们需要从其他构造函数调用空的默认构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.util.LinkedList 类中,定义了2个构造函数,默认构造函数和参数化构造函数接受一个集合。在查看实现时,我看到参数化的构造函数具有 this(),基本上是在调用默认构造函数。

In java.util.LinkedList class, there are 2 constructors defined, default and parameterized that accepts a collection. When I looked at the implementation, I see that the parameterized constructor has this(), basically is calling the default constructor.

但是默认构造函数为空。因此,我很好奇这是否是调用默认构造函数的最佳实践,即使默认构造函数为空或背后没有任何原因。如果不调用默认构造函数,会发生什么情况。

But the default constructor is empty. So I was just curious if this is a best practice to call default constructor even if its empty or is there any reasoning behind this. What happens if we don't call default constructor.

GrepCode LinkedList实现

推荐答案

这并不是公认的良好做法。也许默认的构造函数在以前的版本中做了什么。也许开发人员认为这是一个好主意,因此,如果在默认构造函数中完成了其他一些操作,那么在其他构造函数中也将进行其他操作。如果您查看ArrayList,您会发现做出了不同的选择。

It's not really a recognized good practice. Maybe the default constructor did something in previous versions. Maybe the developer thought it would be a good idea, so that if something additional is done in the default constructor, it will also be done in the other one. If you look at ArrayList, you'll see that a different choice was made.


如果我们不调用默认构造函数会发生什么。

What happens if we don't call default constructor.

不调用默认构造函数。超类no-arg构造函数被隐式调用。

The default constructor is just not called. The superclass no-arg constructor is called implicitely.

这篇关于LinkedList数据结构,为什么我们需要从其他构造函数调用空的默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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