[英]Java EE 6 - Why do I need default constructor and how to define optional parameters?
I'm trying to learn Java and I've got everything setup and I'm running. 我正在尝试学习Java,并且已经完成所有设置,并且正在运行。 I created a constructor for my first model and went to setup a constructor function that accepts a parameter. 我为第一个模型创建了一个构造函数,然后设置了一个接受参数的构造函数。 I added this function: 我添加了此功能:
public Guest(String name) {
this.name = name;
this.signingDate = new Date(System.currentTimeMillis());
}
Netbeans threw an error saying I needed a default constructor. Netbeans抛出一个错误,说我需要一个默认的构造函数。 The error went away when I changed it to: 当我将其更改为时,错误消失了:
public Guest() {
}
public Guest(String name) {
this.name = name;
this.signingDate = new Date(System.currentTimeMillis());
}
I can't imagine that this is how you create optional parameters within Java. 我无法想象这就是您如何在Java中创建可选参数。 So it brings up 2 questions: 因此它提出了两个问题:
实体类必须具有无参数的public或protected构造函数。
Another scenario where you'd need no-arg constrcutors is when you have subclasses. 您需要无参数构造函数的另一种情况是当您有子类时。
For example, 例如,
class Car {
Engine engine;
Car(Engine e)
{
// do something
}
}
And you have a subclass, 你有一个子类,
class Benz extends Car
{
Benz()
{
}
}
Now you can't instantiate a Benz
object. 现在您无法实例化Benz
对象。 The reason is, when a new Benz()
instantiation is attempeted, the parent constructor is first invoked. 原因是,当尝试使用new Benz()
实例时,将首先调用父构造函数。
ie, 即
class Benz extends Car
{
Benz()
{
super(); //implicitly added
}
}
By default, it tries to invoke a no-arg constructor of the super-class. 默认情况下,它尝试调用超类的无参数构造函数。 So you'd need one, unless you have an explicit super(arg) call like this: 因此,除非您有一个明确的super(arg)调用,否则您将需要一个:
class Benz extends Car
{
Benz()
{
super(new Engine());
}
}
Note: A simple Java rule to remember A no-arg constructor is provided by default as long as you don't have another constructor with argument. 注意:记住一个简单的Java规则,默认情况下会提供no-arg构造函数,只要您没有另一个带参数的构造函数即可。
ie 即
class Car{}
is equivalent to
相当于
class Car{ Car() }
But when you write, 但是当你写的时候
class Car{ Car(Engine e){} }
there is no no-arg constructor provided by default.
默认情况下,没有提供no-arg构造函数。
You'll have to explicitly code
您必须显式编码
class Car{ Car(){} Car(Engine e)}
if you needed it.
如果需要的话。
Yes. 是。 Java supports method / constructor overloading. Java支持方法/构造函数重载。
You don't "need" the default constructor. 您不需要“需要”默认构造函数。 However, NetBeans is being helpful to you, and informing you that you need one because it assumes you are creating a bean - which requires the option to be created without a constructor requiring parameters - because the calling code wouldn't know what to populate into these. 但是,NetBeans对您有所帮助,并通知您您需要一个,因为它假定您正在创建一个Bean-要求创建该选项而无需构造函数需要参数-因为调用代码不知道要填充的内容这些。
For "optional" parameters, you can define as many constructors as you would need - with each one omitting one or more of the "optional" parameters. 对于“可选”参数,可以定义所需数量的构造函数-每个构造函数都忽略一个或多个“可选”参数。
Why do I need a default constructor? 为什么需要默认构造函数?
I think you are trying to call new Guest()
and have only one constructor in Guest class that is Guest(String arg)
. 我认为您正在尝试调用new Guest()
并且在Guest类中只有一个构造函数是Guest(String arg)
。 And that's why it is showing error that you need to create a constructor with no arguments that is default constructor of a class. 这就是为什么它显示错误,您需要创建一个不带参数的构造函数(它是类的默认构造函数)的原因。
How do I create optional parameters? 如何创建可选参数?
If you want to make a constructor with 0 more arguments you need to use ...
in argument. 如果要使用更多的参数作为构造函数,则需要在参数中使用...
Ex: 例如:
public class Testing {
public Testing(String... str) {
}
public static void main(String[] args) {
Testing testing = new Testing();
Testing testing1 = new Testing("1");
Testing testing2 = new Testing("2","1");
}
}
If you're using a POJO (Plain Old Java Object) than you need a default constructor only if you create an instance of this object by invoking no-argument constructor like Guest g = new Guest()
. 如果您使用的是POJO(普通的Java旧对象),则仅当通过调用无参数的构造函数(如Guest g = new Guest()
创建该对象的实例时,才需要默认构造函数。
That's pretty obvious - you want to use no-argument constructor - you need to have one. 这很明显-您想使用无参数构造函数-您需要一个。
If you don't specify any constructor, the compiler will generate one (default, no-argument) for you. 如果您不指定任何构造函数,则编译器将为您生成一个(默认,无参数)。 If you specify at least one constructor by yourself - the compiler won't generate any as it assumes that you know what you're doing. 如果您自己指定至少一个构造函数-编译器将不会生成任何构造函数,因为它假定您知道自己在做什么。
However, if you're creating a JPA entity (so you have @Entity
at the top of your class) than you need a default constructor as it's a JPA requirement . 但是,如果要创建JPA实体 (因此类的顶部有@Entity
,则需要默认构造函数,因为这是JPA的要求 。
It's because instances of your class are created by the JPA provider when you fetch them from the database. 这是因为从数据库中获取类的实例是由JPA提供程序创建的。 The JPA provider has to have a way to instantiate your Entity. JPA提供者必须有一种实例化您的实体的方法。
If you'd have only parametrized constructor - how the JPA would know what parameters should it pass? 如果您只有参数化的构造函数-JPA如何知道应该传递哪些参数?
There are no optional parameters in Java like there are in ie PHP world where you can define myMethod($var1, $var2 = null, $var3 = 2);
Java中没有可选参数,例如在PHP世界中,您可以定义myMethod($var1, $var2 = null, $var3 = 2);
. 。 In Java world we use overloaded methods and varargs . 在Java世界中,我们使用重载方法和varargs 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.