为什么Java没有初始化列表像C ++? [英] Why doesn't Java have initializer lists like in C++?

查看:226
本文介绍了为什么Java没有初始化列表像C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,可以使用初始化器列表在构造函数开始运行之前初始化类的字段。例如:

In C++, you can use an initializer list to initialize the class's fields before the constructor begins running. For example:

Foo::Foo(string s, double d, int n) : name(s), weight(d), age(n) {
    // Empty; already handled!
}



我很好奇为什么Java没有类似的功能。根据核心Java:第1卷:


C ++使用此特殊语法调用字段构造函数。在Java中,没有必要,因为对象没有子对象,只有指向其他对象的指针。

C++ uses this special syntax to call field constructors. In Java, there is no need for it because objects have no subobjects, only pointers to other objects.

这里是我的问题: / p>

Here are my questions:


  1. 因为对象没有子对象?我不明白一个子对象是什么(我试过查找它);它们意味着扩展超类的子类的实例化

  1. What do they mean by "because objects have no subobjects?" I don't understand what a subobject is (I tried looking it up); do they mean an instantiation of a subclass which extends a superclass?

至于为什么Java没有初始化列表像C ++,我想假设原因因为所有字段都已在Java中被默认初始化,并且因为Java使用 super 关键字来调用超级(或C ++ lingo中的base)构造函数。这是正确的吗?

As for why Java does not have initializer lists like C++, I would assume that the reason is because all fields are already initialized by default in Java and also because Java uses the super keyword to call the super(or base in C++ lingo)-class constructor. Is this correct?


推荐答案

Java中不存在的一些语言特性或在Java中不同的工作:

In C++, initializer lists are necessary because of a few language features that are either not present in Java or work differently in Java:


  1. const :在C ++中,可以定义一个标记为 const 的字段,不能被分配给初始化列表。 Java有 final 字段,但您可以分配给构造函数主体中的 final 字段。在C ++中,在构造函数中赋予 const 字段是非法的。

  1. const: In C++, you can define a fields that are marked const that cannot be assigned to and must be initialized in the initializer list. Java does have final fields, but you can assign to final fields in the body of a constructor. In C++, assigning to a const field in the constructor is illegal.

/ strong>:在C ++中,引用(而不是指针)必须初始化以绑定到某个对象。在没有初始化器的情况下创建引用是非法的。在C ++中,您指定的方式是使用初始化器列表,因为如果您要在不首先初始化它的情况下引用构造函数主体中的引用,那么您将使用未初始化的引用。在Java中,对象引用的行为类似于C ++指针,可以在创建后分配给它。它们只是默认为 null

直接子对象。在C ++中,对象可以直接包含对象作为字段,而在Java对象中只能保存对这些对象的引用。也就是说,在C ++中,如果你声明一个有 string 作为成员的对象,那个字符串的存储空间直接被构建到对象本身的空间中,而在Java中,你只是获得空间来引用存储在其他地方的一些其他 String 对象。因此,C ++需要提供一种方式给你这些子对象的初始值,因为否则他们只是保持未初始化。默认情况下,它使用这些类型的默认构造函数,但如果你想使用不同的构造函数或没有默认构造函数可用,初始化器列表给你一个方法来绕过这个。在Java中,你不需要担心这个,因为引用将默认为 null ,然后你可以分配它们来引用你实际想要他们引用的对象至。如果你想使用非默认构造函数,那么你不需要任何特殊的语法;

Direct subobjects. In C++, an object can contain object directly as fields, whereas in Java objects can only hold references to those objects. That is, in C++, if you declare an object that has a string as a member, the storage space for that string is built directly into the space for the object itself, while in Java you just get space for a reference to some other String object stored elsewhere. Consequently, C++ needs to provide a way for you to give those subobjects initial values, since otherwise they'd just stay uninitialized. By default it uses the default constructor for those types, but if you want to use a different constructor or no default constructor is available the initializer list gives you a way to bypass this. In Java, you don't need to worry about this because the references will default to null, and you can then assign them to refer to the objects you actually want them to refer to. If you want to use a non-default constructor, then you don't need any special syntax for it; just set the reference to a new object initialized via the appropriate constructor.

在少数情况下,Java可能需要初始化程序列表(例如,调用超类构造函数或给它的字段赋予默认值),这是通过另外两个语言特性来处理的:调用超类构造函数的 super 关键字,事实,Java对象可以在它们被声明的点给它们的字段默认值。由于C ++具有多重继承,只需要一个 super 关键字就不会明确地引用单个基类,而在C ++ 11 C ++之前不支持默认初始化

In the few cases where Java might want initializer lists (for example, to call superclass constructors or give default values to its fields), this is handled through two other language features: the super keyword to invoke superclass constructors, and the fact that Java objects can give their fields default values at the point at which they're declared. Since C++ has multiple inheritance, just having a single super keyword wouldn't unambiguously refer to a single base class, and prior to C++11 C++ didn't support default initializers in a class and had to rely on initializer lists.

希望这有助于!

这篇关于为什么Java没有初始化列表像C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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