简体   繁体   English

使用Java中的默认构造函数复制构造函数

[英]Copy Constructor with Default Constructor in Java

I need to have a copy constructor in my class as i need to create duplicate objects. 我需要在类中有一个复制构造函数,因为我需要创建重复的对象。 I believe that if i will create a copy constructor, I will have to specify the non parameterized constructor too as Java will no longer provide the default constructor. 我相信,如果我将创建副本构造函数,那么我也必须指定非参数化的构造函数,因为Java将不再提供默认的构造函数。

I don't want to touch the default constructor as that is what i being used in the code everywhere. 我不想触摸默认构造函数,因为这是我在各处代码中使用的构造函数。 Is there a workaround to have either a copy constructor or something like it without defining the basic constructor. 是否有一种解决方法,可以在没有定义基本构造函数的情况下使用复制构造函数或类似的东西。

No, if you want to have both a parameterless constructor and a constructor with parameters, you need to declare them both. 不,如果要同时具有无参数构造函数具有参数的构造函数,则需要同时声明它们。 It's very easy to declare the parameterless constructor though: 声明无参数构造函数非常容易:

public YourClassName() {
}

The super(); super(); is implicit. 是隐式的。

That will behave exactly the same way as the default constructor would, although it won't necessarily have the same access as the default constructor. 尽管它不一定具有与默认构造函数相同的访问权限,但其行为与默认构造函数的行为完全相同。 (You can change it from being public if you want, of course.) From the JLS section 8.8.9 : (当然,您可以根据需要将其从public更改。)从JLS第8.8.9节中

If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared. 如果一个类不包含构造函数声明,则隐式声明一个没有形式参数且没有throws子句的默认构造函数。

If the class being declared is the primordial class Object, then the default constructor has an empty body. 如果要声明的类是原始类Object,则默认构造函数的主体为空。 Otherwise, the default constructor simply invokes the superclass constructor with no arguments. 否则,默认构造函数将简单地调用不带参数的超类构造函数。

It is a compile-time error if a default constructor is implicitly declared but the superclass does not have an accessible constructor (§6.6) that takes no arguments and has no throws clause. 如果隐式声明了默认构造函数,但超类没有可访问的构造函数(第6.6节),该构造函数不带参数且不包含throws子句,则这是编译时错误。

In a class type, if the class is declared public, then the default constructor is implicitly given the access modifier public (§6.6); 在类类型中,如果将该类声明为public,则默认构造函数被隐式赋予访问修饰符public(第6.6节); if the class is declared protected, then the default constructor is implicitly given the access modifier protected (§6.6); 如果该类被声明为受保护的,则默认构造函数被隐式赋予访问修饰符保护(第6.6节); if the class is declared private, then the default constructor is implicitly given the access modifier private (§6.6); 如果该类被声明为私有,则默认构造函数被隐式赋予访问修饰符私有(第6.6节); otherwise, the default constructor has the default access implied by no access modifier. 否则,默认构造函数具有无访问修饰符暗含的默认访问权限。

If you really do not want to write the default constructor, you can do: 如果您确实不想编写默认的构造函数,则可以执行以下操作:

public static MyClass create(MyClass original) {
    // return the new instance
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM