无法将接口转换为已实现的类

时间:2016-04-12 01:55:35

标签: java interface casting

这是我的班级:

public class LinkedListSet implements Set {
    private class Node  //much easier as a private class; don't have to extend
    {
        private int data;
        private Node next;
        public Node (){}
        public Node (int x)
        {
            data = x;
        }

        public int data()
        {
            return data;
        }

        public Node next()
        {
        return next;
        }

    }

    private Node first;
    private int Size;
    private int whichList; //used to identify the particular LL object

这是我的界面:

public interface Set {
    public boolean isEmpty();            
    public void makeEmpty();            
    public boolean isMember(int x); 
    public void add(int x);    
    public void remove(int y);            
    public void union(Set other, Set result); 
    public void intersection (Set other, Set result); 
    public void difference (Set other, Set result); 
    @Override
    public String toString(); 

    @Override
    public boolean equals(Object other); 

    public void setList(int i); //i added this to use it as an identifier for each
                            //list element in the set array

    public String getListId(); //these two extra methods make life easier

}

我有一个像这样的方法(在LinkedListSet类中):

 public void difference (Set other, Set result)
 {
     if (other.isEmpty())
     {
         System.out.println("The set is empty before cast");
     }
     LinkedListSet othr = (LinkedListSet) other;
     LinkedListSet res = (LinkedListSet) result;
     if (this.isEmpty() || othr.isEmpty())
     {
         if (othr.isEmpty())
             System.out.println("The set is empty after cast");
         if (this.isEmpty())
             System.out.println("This is also empty");
         return;
     }
     differenceHelper(this.first, othr.first, res);
     result = res;
 }// the print statements were added for debugging

问题是,在上面的方法中,我无法将Set Other强制转换为其链表实现。当我在主程序中调用此方法时,该参数实际上是类型链表(因此我没有明显的错误)。

但是,所有实例变量都是null。在我投射它之前和之后列表是空的(当它实际上不是空的时候)。我知道这是因为界面不包含有关Nodes的任何信息,但除了编辑界面以合并Node之外,还有什么我可以做的吗?

我希望我已经说清楚了。任何帮助将不胜感激。

编辑: 在主程序中,我创建了一个Sets数组。

    Set[] sets = new Set[7];
    for (int i = 0; i< sets.length; i++) //initialize each element
    {
        sets[i] = new LinkedListSet();
    }

每个列表都有包含数据值的节点,稍后会在代码中添加...

然后我称之为差异方法。

      sets[0].difference(sets[1], sets[4])

sets [1] .isEmpty由于某种原因返回true(即使它不是)。

如果我要做的事情如下: System.out.println(sets [1] .first.data())我没有任何问题。 由于某些原因,当参数传递给差异方法时,所有值都为空。

    public boolean isEmpty()
{
  return first == null;
}

1 个答案:

答案 0 :(得分:0)

我使用以下代码测试了您要执行的操作,我发现没有问题:

import org.junit.Test;

public class RandomCastTest {

    public interface Set {
        boolean isEmpty();

        void add(int x);

        void difference(Set other, Set result);

        @Override
        String toString();

        @Override
        boolean equals(Object other);
    }

    public class LinkedListSet implements Set {
        private class Node  //much easier as a private class; don't have to extend
        {
            private int data;
            private Node next;

            public Node() {
            }

            public Node(int x) {
                data = x;
            }

            public int data() {
                return data;
            }

            public Node next() {
                return next;
            }

            public void next(Node node) {
                next = node;
            }
        }

        private Node first;
        private int Size;
        private int whichList; //used to identify the particular LL object

        @Override
        public boolean isEmpty() {
            return first == null;
        }

        @Override
        public void add(int x) {
            Node node = new Node(x);

            if (first == null) {
                first = node;
            } else {
                Node currentNode;
                Node nextNode = first;
                do {
                    currentNode = nextNode;
                    nextNode = currentNode.next();
                } while (nextNode != null);

                currentNode.next(node);
            }
            Size++;
        }

        @Override
        public void difference(Set other, Set result) {
            if (other.isEmpty()) {
                System.out.println("The set is empty before cast");
            }
            LinkedListSet othr = (LinkedListSet) other;
            LinkedListSet res = (LinkedListSet) result;
            if (this.isEmpty() || othr.isEmpty()) {
                if (othr.isEmpty())
                    System.out.println("The set is empty after cast");
                if (this.isEmpty())
                    System.out.println("This is also empty");
                return;
            }
            result = res;
        }
    }

    @Test
    public void test() {
        Set[] sets = new Set[7];
        for (int i = 0; i < sets.length; i++) {
            sets[i] = new LinkedListSet();
        }

        for (int i = 0; i < 5; i++) {
            sets[1].add(i);
        }

        for (int i = 5; i < 10; i++) {
            sets[0].add(i);
        }

        sets[0].difference(sets[1], sets[4]);
        // ... find difference
    }
}

为了简化,我从界面中删除了未实现的方法。还添加了add方法实现。请看看它是否适合你。