gpt4 book ai didi

java - 一个类可以同时具有公共(public)和私有(private)构造函数吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:29:30 26 4
gpt4 key购买 nike

我遇到了一个场景,我需要一个公共(public)构造函数和一个私有(private)构造函数。需要一个私有(private)构造函数来设置类型为私有(private)内部类的私有(private)字段。这是鼓舞还是气馁?对于下面列出的场景,还有什么更好的解决方案?

请阅读评论,它更有意义地支持我的问题。谢谢,

public class CopyTree { 
private TreeNode root;

public Copytree() { }

// private CopyTree(TreeNode root) { this.root = root; }

private static class TreeNode {
TreeNode left;
TreeNode right;
Integer element;
TreeNode(TreeNode left, TreeNode right, Integer element) {
this.left = left;
this.right = right;
this.element = element;
}
}

public CopyTree copyTree() {
CopyTree ct = new CopyTree();
ct.root = copyTree(root); // <---- QUESTION: Any cleaner solution ??
// cleaner solution appears to be a private constructor
// eg: CopyTree ct = new CopyTree(copyTree(root)); But can public and private constructor coexist together ?
return ct;
}

private TreeNode copyTree(TreeNode binaryTree) {
TreeNode copy = null;
if (binaryTree != null) {
copy = new TreeNode(null, null, binaryTree.element);
copy.left = copyTree(binaryTree.left);
copy.right = copyTree(binaryTree.right);
}
return copy;
}

最佳答案

Can a class have both public and private constructor?

是的,这是可能的。

A private constructor is needed to set the private field whose type is a private inner class. Is this encouraged or discouraged ?

视情况而定。您是否希望其他类初始化您的对象的状态。在这里,我认为您已经创建了 CopyTree 类以返回私有(private)类 Tree 的副本。因此 TreeNode 类将被封装,因此您可以选择使用私有(private)构造函数捕获惯用语。

what is a better solution for a scenario listed below ?

在我看来 private constructor capture idiom 是更好的解决方案。

更多信息:

您可以搜索 private constructor capture idiom .

Java Puzzlers 的解决方案 53 中给出了一个示例:

谜题 53:做你喜欢的事

Now it's your turn to write some code. Suppose that you have a library class called Thing whose sole constructor takes an int parameter:

public class Thing {    
public Thing(int i) { ... }
...
}

A Thing instance provides no way to get the value of its constructor parameter. Because Thing is a library class, you have no access to its internals, and you can't modify it. Suppose that you want to write a subclass called MyThing, with a constructor that computes the parameter to the superclass constructor by invoking the method SomeOtherClass.func(). The value returned by this method changes unpredictably from call to call. Finally, suppose that you want to store the value that was passed to the superclass constructor in a final instance field of the subclass for future use. This is the code that you'd naturally write:

public class MyThing extends Thing {
private final int arg;
public MyThing() {
super(arg = SomeOtherClass.func());
...
}
...
}

不幸的是,这是不合法的。如果您尝试编译它,您将收到如下所示的错误消息:

MyThing.java:
can't reference arg before supertype constructor has been called
super(arg = SomeOtherClass.func());
^

How can you rewrite MyThing to achieve the desired effect? The MyThing() constructor must be thread-safe: Multiple threads may invoke it concurrently.

解决方案 53:做你的事

You could try to stash the result of the invocation SomeOtherClass.func() in a static field prior to invoking the Thing constructor. This solution is workable but awkward. In order to achieve thread-safety, you must synchronize access to the stashed value, which requires unimaginable contortions. Some of these contortions can be avoided by using a thread-local static field (java.util.ThreadLocal), but a much better solution exists. The preferred solution is inherently thread-safe as well as elegant. It involves the use of second, private constructor in MyThing:

public class MyThing extends Thing {
private final int arg;

public MyThing() {
this(SomeOtherClass.func());
}

private MyThing(int i) {
super(i);
arg = i;
}
}

This solution uses an alternate constructor invocation. This feature allows one constructor in a class to chain to another constructor in the same class. In this case, MyThing() chains to private constructor MyThing(int), which performs the required instance initialization. Within the private constructor, the value of expression SomeOtherClass.func() has been captured in the parameter i and can be stored in the final field param after the superclass constructor returns.

关于java - 一个类可以同时具有公共(public)和私有(private)构造函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17519702/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com