在java中调用类的多个实例时出现问题

时间:2010-03-02 14:19:08

标签: java

我在调用已编码的类的多个实例时遇到问题(Tree,TreeNode) 在主方法中,系统将给出输出c d j c d j,即使两个树明显是不同的树。 如果我要分开两个postOrder()调用(每个调用树被推入堆栈后调用)

    Stack<Tree> alphaStack = new Stack<Tree>();
    TreeNode a = new TreeNode('i');
    Tree tree = new Tree(a);
    TreeNode newleft = new TreeNode('a');
    TreeNode newright = new TreeNode('b');
    tree.setLeft(a, newleft);
    tree.setRight(a, newright);
    alphaStack.push(tree);
    Tree.postOrder(alphaStack.pop().getRoot());

    TreeNode b = new TreeNode('j');
    Tree newtree = new Tree(b);
    TreeNode left = new TreeNode('c');
    TreeNode right = new TreeNode('d');
    newtree.setLeft(b, left);
    newtree.setRight(b, right);
    alphaStack.push(newtree);

    Tree.postOrder(alphaStack.pop().getRoot());

输出将是b i c d j。

这是否意味着我的班级没有重复,而是在制作新树时重复使用?

以下是代码:

  

import java.util.Stack;

     

public class mast_score {

public static void main(String[] args){
    Stack<Tree> alphaStack = new Stack<Tree>();
    TreeNode a = new TreeNode('i');
    Tree tree = new Tree(a);
    TreeNode newleft = new TreeNode('a');
    TreeNode newright = new TreeNode('b');
    tree.setLeft(a, newleft);
    tree.setRight(a, newright);
    alphaStack.push(tree);

    TreeNode b = new TreeNode('j');
    Tree newtree = new Tree(b);
    TreeNode left = new TreeNode('c');
    TreeNode right = new TreeNode('d');
    newtree.setLeft(b, left);
    newtree.setRight(b, right);
    alphaStack.push(newtree);

    Tree.postOrder(alphaStack.pop().getRoot());
    Tree.postOrder(alphaStack.pop().getRoot());
     

}}

TreeNode的代码

  

公共类TreeNode {       对象项;

TreeNode parent;
TreeNode left;
TreeNode right;

public TreeNode (Object item) {
    this.item = item;
    parent = null;
    left = null;
    right = null;
}

public TreeNode getParent(TreeNode current) throws ItemNotFoundException
{
    if(current == null) throw new ItemNotFoundException("No parent");
    if(current.parent == null) throw new ItemNotFoundException("This
    is the root");
    else return current.parent;
}
public TreeNode getLeft(TreeNode current) throws ItemNotFoundException
{
    if(current == null) throw new ItemNotFoundException("No left or
    right child");
    if(current.left == null) throw new ItemNotFoundException("No left
    child");
    else return current.left;
}

public TreeNode getRight(TreeNode current) throws ItemNotFoundException
{
    if(current == null) throw new ItemNotFoundException("No left or
    right child");
    if(current.right == null) throw new ItemNotFoundException("No
    right child");
    else return current.right;
}

public Object getElement() throws ItemNotFoundException {
    if(this.item == null) throw new ItemNotFoundException("No such
    node");
    else return this.item;
} }

Tree类的代码

  

import java.util。*;

     

public class Tree {

static TreeNode root;
int size;

public Tree() {
    root = null;
}

public Tree(TreeNode root) {
    Tree.root = root;
}

public TreeNode getRoot() {
    return this.root;
}

public int getLvl(TreeNode node) {
    return node.lvlCount;
}

public void setLeft(TreeNode node, TreeNode left) {
    node.left = left;
}

public void setRight(TreeNode node, TreeNode right) {
    node.right = right;
}
public static void postOrder(TreeNode root) {
    if (root != null) {

        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.item + " ");

    } else {
        return;
    }

}

public static int getSize(TreeNode root) {
    if (root != null) {
        return 1 + getSize(root.left) +
    getSize(root.right);
    } else {
        return 0;
    }
}

public static boolean isEmpty(Tree Tree) {
    return Tree.root == null;
} }

1 个答案:

答案 0 :(得分:6)

您的问题出现在Tree班级:

static TreeNode root;

您应该删除static一词,并将Tree.root替换为this.root

添加关键字static会导致变量root在程序中Tree的所有实例之间共享,这不是您想要的。