Java-创建对象而不更改原始对象 - java

是否可以在不更改原始对象的情况下使用其属性创建新对象?

例如 :

public void exampleTests() {
        Tree t = Trees.makeTree(new int[]{2, 3, 4, 4, 1});//creating tree
        assertTrue(t.contains(4)); //check if 4 is a node
        assertFalse(t.contains(6));//check if 6 is a node
        assertEquals(4, t.size()); //return size-nodes number (only different digits)

        Tree t2 = t.add(6).add(7).add(6); // obj 2 take obj 1 and add 6 and 7 to it
        assertFalse(t.contains(6)); // the first object should have no 6
        assertTrue(t2.contains(6)); // the second object should have 6

树木类:

public  class Trees  {

    public  static Tree makeTree(int[] elements) {

        Tree tree = new Nodes();
        for (int i : elements) {
            tree.add(i);
        }
        return tree;
    }

}

树形界面

public interface Tree {


    public  Tree add(int i);

    public boolean contains(int i);

    public int size();

    public String elementsAsString();

节点类别:

public class Node  {
    int i;
    Node left;
    Node right;

    public Node(int data) {
        this.i = data;
        left = null;
        right = null;
    }
}

节点类:

public class Nodes implements Tree {


    private    Node root;

    public Nodes() {
        this.root = null;
    }

    @Override
     public Nodes add(int i) {
        root = insertNode(root, new Node(i));
        return new Nodes();
    }

     private Node insertNode(Node currentParent, Node newNode) {

        if (currentParent == null) {
            return newNode;
        } else if (newNode.i > currentParent.i) {
            currentParent.right = insertNode(currentParent.right, newNode);
        } else if (newNode.i < currentParent.i) {
            currentParent.left = insertNode(currentParent.left, newNode);
        }
        return currentParent;
    }

我们用Java术语称之为什么?

参考方案

您将需要创建原始对象的副本。

一种方法是使用复制构造函数:

public Tree (Tree other) {
    // copy all the properties of other to the new object
}

然后改变

Tree t2 = t.add(6).add(7).add(6);

Tree t2 = new Tree(t).add(6).add(7).add(6); 

请注意,如果Tree的成员包括引用类型(即对其他对象的引用),则必须决定是否也创建这些对象的新副本。如果仅复制引用,则将获得原始对象的浅表副本,这可能会导致问题。

编辑:

由于Tree似乎是一个接口,因此您必须在实现它的类中创建一个副本构造函数:

public Nodes (Tree other) {
    // copy all the properties of other to the new object
}

然后,您可以直接创建副本:

Tree t2 = new Nodes(t).add(6).add(7).add(6); 

或通过工厂方法:

Tree t2 = Trees.makeTree(t).add(6).add(7).add(6);

其中makeTree是:

public  static Tree makeTree(Tree source) {

    Tree tree = new Nodes(source);
    return tree;
}

请注意,public Nodes (Tree other)现在不完全是复制构造函数-它比复制构造函数更通用,因为它可以接受Tree接口的任何实现并创建一个包含相同数据的新Nodes实例。

Java:线程池如何将线程映射到可运行对象 - java

试图绕过Java并发问题,并且很难理解线程池,线程以及它们正在执行的可运行“任务”之间的关系。如果我创建一个有10个线程的线程池,那么我是否必须将相同的任务传递给池中的每个线程,或者池化的线程实际上只是与任务无关的“工人无人机”可用于执行任何任务?无论哪种方式,Executor / ExecutorService如何将正确的任务分配给正确的线程? 参考方案 …

JAVA:字节码和二进制有什么区别? - java

java字节代码(已编译的语言,也称为目标代码)与机器代码(当前计算机的本机代码)之间有什么区别?我读过一些书,他们将字节码称为二进制指令,但我不知道为什么。 参考方案 字节码是独立于平台的,在Windows中运行的编译器编译的字节码仍将在linux / unix / mac中运行。机器代码是特定于平台的,如果在Windows x86中编译,则它将仅在Win…

java:继承 - java

有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…

Java:BigInteger,如何通过OutputStream编写它 - java

我想将BigInteger写入文件。做这个的最好方式是什么。当然,我想从输入流中读取(使用程序,而不是人工)。我必须使用ObjectOutputStream还是有更好的方法?目的是使用尽可能少的字节。谢谢马丁 参考方案 Java序列化(ObjectOutputStream / ObjectInputStream)是将对象序列化为八位字节序列的一种通用方法。但…

Java DefaultSslContextFactory密钥库动态更新 - java

我有一个使用org.restlet.engine.ssl.DefaultSslContextFactory的现有应用程序和一个在服务器启动时加载的密钥库文件。我有另一个应用程序,该应用程序创建必须添加的证书服务器运行时动态地更新到密钥库文件。为此,我在代码中创建了证书和私钥,然后将其写入到目录。该目录由bash脚本监视,该脚本检查是否有新文件,如果出现,它将…