构造函数和零参数构造函数之间的区别(Java)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15490010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between a constructor and a zero parameter constructor (Java)
提问by choloboy7
Can anyone tell me what the difference (conceptually) is between a constructor and a zero parameter constructor is? Example:
谁能告诉我构造函数和零参数构造函数之间的区别(概念上)是什么?例子:
How would this implementation affect a program
此实施将如何影响程序
public Person() {
firstName = "UNKNOWN";
lastName = "UNKNOWN";
gender = '?';
retired = false;
age = 0;
address = null;
}
compared to say... this:
相比说……这个:
public Person(String f, String l, int a, char g) {
firstName = f;
lastName = l;
age = a;
gender = g;
retired = false;
address = null;
}
edit: please ignore the number of parameters...
编辑:请忽略参数的数量...
回答by lichengwu
If you don't define a constructor for a class, a default parameterless constructor is automatically created by the compiler. The default constructor calls the default parent constructor (super()) and initializes all instance variables to default value (zero for numeric types, null for object references, and false for booleans).
如果没有为类定义构造函数,编译器会自动创建一个默认的无参数构造函数。默认构造函数调用默认父构造函数 (super()) 并将所有实例变量初始化为默认值(数字类型为零,对象引用为 null,布尔值为 false)。
Default constructor is created only if there are no constructors. If you define any constructor for your class, no default constructor is automatically created.
仅当没有构造函数时才创建默认构造函数。如果为类定义任何构造函数,则不会自动创建默认构造函数。
Can anyone tell me what the difference (conceptually) is between a constructor and a zero parameter constructor is?
谁能告诉我构造函数和零参数构造函数之间的区别(概念上)是什么?
No difference.
没有不同。
回答by Eric Jablow
Also, a good style is to have only one constructor that does all the real work, even if you have more than one constructor. The term designated constructorcomes from Objective-C, but it's still a good idea. Suppose you write this:
此外,一种好的风格是只有一个构造函数来完成所有实际工作,即使您有多个构造函数。术语指定构造函数来自Objective-C,但它仍然是一个好主意。假设你这样写:
public Person(String f, String l, int a, char g) {
// Use more expressive argument names.
firstName = f;
lastName = l;
age = a;
gender = g;
retired = false;
address = null;
}
Then, rewrite your no-argument constructor as
然后,将您的无参数构造函数重写为
public Person() {
this(null, null, 0, '?');
// Invoke the designated constructor with those arguments.
retired = false;
address = null;
}
If the first statement of a constructor is this(...)
, then the constructor is invoking one of the other constructors of the class. Similarly, if the first statement of a constructor is super(...)
, then one is invoking a constructor of the parent class. If one does neither, than super()
is assumed.
如果构造函数的第一条语句是this(...)
,则构造函数正在调用类的其他构造函数之一。同样,如果构造函数的第一条语句是super(...)
,则调用父类的构造函数。如果两者都不做,super()
则假设。
Then, since retired
and address
is set the same way in both constructors, initialize them in their declarations:
然后,由于retired
和address
在两个构造函数中的设置方式相同,因此在它们的声明中初始化它们:
private boolean retired = false;
private String address = null; // If it's a String; it could be its own object type.
And then, since false
and null
are the default values for instance variable initialization, replace those two lines with:
然后,由于false
和null
是实例变量初始化的默认值,将这两行替换为:
private boolean retired;
private String address;
Later on, you can add error checking. For example, people do not have negative ages, so inserting the statement
稍后,您可以添加错误检查。例如,人们没有负年龄,因此插入语句
if (a < 0) {
throw new IllegalArgumentException("A Person cannot have a negative age: "
+ a);
}
at the beginning of the designated constructor would be useful. This way, you need do that only once.
在指定构造函数的开头会很有用。这样,您只需要做一次。
When you learn about Java enums, you can then make your gender variable more precise by writing one:
当您了解 Java 枚举时,您可以通过编写以下代码来使您的性别变量更加精确:
// Gender is a grammatical term, Sex is a biological term.
public enum Sex {
MALE, FEMALE;
}
and using Sex
instead of char
for your gender
variable.
并使用Sex
代替char
您的gender
变量。
回答by Pochemuchkin
A zero-parameters constructor will be auto-created when there are no other constructors defined. Constructors with parameters are used for a customized initialization of objects.
当没有定义其他构造函数时,将自动创建零参数构造函数。带参数的构造函数用于自定义对象的初始化。
回答by Makoto
For the first constructor, any time that you want to instantiate a Person
, it will always start with the values defined in the constructor. You will have to use setters to modify that.
对于第一个构造函数,任何时候你想实例化 a Person
,它总是从构造函数中定义的值开始。您将不得不使用 setter 来修改它。
For the second constructor, any time that you want to instantiate a Person
, you must pass through the appropriate parameters. It means that you can create your objects without having to use a setter to change the value later on.
对于第二个构造函数,任何时候要实例化 a Person
,都必须传递适当的参数。这意味着您可以创建您的对象,而无需使用 setter 稍后更改值。
回答by Henry Leu
- If you don't define a constructor, the default constructor (which has no parameters) will be generated by default.
- If you have any user-defined constructor, the default constructor will no be generated at all.
- whatever, you need a constructor unless your class is utility class.
- 如果没有定义构造函数,默认会生成默认构造函数(没有参数)。
- 如果您有任何用户定义的构造函数,则根本不会生成默认构造函数。
- 无论如何,除非您的类是实用程序类,否则您需要一个构造函数。
回答by phatfingers
A constructor is called when instantiating an object. A zero parameter (or "no-arg") constructor is one that requires no arguments. A class can have multiple constructors, each with different arguments.
实例化对象时会调用构造函数。零参数(或“无参数”)构造函数是不需要参数的构造函数。一个类可以有多个构造函数,每个构造函数都有不同的参数。
Constructors without arguments are particularly helpful for tools that use reflection to populate an object's properties. The default no-arg constructor is used to construct the object, then its properties are populated by matching the names used in their setters (aka mutators).
没有参数的构造函数对于使用反射来填充对象属性的工具特别有用。默认的无参数构造函数用于构造对象,然后通过匹配它们的 setter(又名 mutators)中使用的名称来填充其属性。
Constructors with arguments are especially well suited for creating immutable objects. The object can be populated once when it is created, and then is guaranteed to not change (provided all the properties are similarly immutable).
带参数的构造函数特别适合创建不可变对象。该对象可以在创建时填充一次,然后保证不会更改(假设所有属性同样不可变)。
回答by Railre
first if you don't have statement constructor the class can aoto create constructor but this constructor not parameter and and initializes all instance variables to default value (zero for numeric types, null for object references, and false for booleans)like this:
首先,如果您没有语句构造函数,该类可以自动创建构造函数,但此构造函数不是参数,并将所有实例变量初始化为默认值(数字类型为零,对象引用为 null,布尔值为 false),如下所示:
public Person() {
firstName = null;
lastName = null;
gender = null;
retired = false;
age = 0;
address = null;
}
but you can override this constructorlike this:
但是你可以像这样覆盖这个构造函数:
public Person() {
firstName = "firstName";
lastName = "lastName";
gender = '?';
retired = true;
age = 0;
address = "someAddress";
}
and overload this constructorlike this:
并像这样重载这个构造函数:
public Person(String f, String l, int a, char g) {
firstName = f;
lastName = l;
age = a;
gender = g;
retired = false;
address = null;
}'