0

我正在按照此处的示例进行操作:

在接口部分中,注意到实现接口的类必须实现接口的所有方法。但是,可以定义一个不实现所有接口方法的类,前提是该类被声明为抽象类。例如,

abstract class X implements Y {
  // implements all but one method of Y
}

class XX extends X {
  // implements the remaining method in Y
}

在这种情况下,类 X 必须是抽象的,因为它没有完全实现 Y,但类 XX 实际上实现了 Y。

这是我的代码:

public interface IRunnable {
public Object[] run(Graph<Object, ?> graph, HashMap<Object, Point> positions, int numIter);
public Object[] runSet(Graph<Object, ?> graph, HashMap<Object, Point> positions, int numIter, int [] itr);
public HashMap<Object, Point> runHelper(Graph<Object, ?> graph,HashMap<Object, Point> positions, int numIter);
public Force calculateForces(Graph<Object, ?> graph, HashMap<Object, Point> positions, Object mainVertex);

}

public abstract class Runnable implements IRunnable {
    public Object[] run(Graph<Object, ?> graph, HashMap<Object, Point> positions, int numIter){
           Object[] res = new Object[2];
           long startTime = System.currentTimeMillis();
           res[0] = runHelper(graph, new HashMap<Object, Point>(positions), numIter);
           long endTime = System.currentTimeMillis();
           res[1] = endTime - startTime;
           return res;
        }
        public Object[] runSet(Graph<Object, ?> graph, HashMap<Object, Point> positions, int numIter, int [] itr){
             System.out.println(itr.length);
             Object[] res = new Object[itr.length];
             for ( int i = 0; i < itr.length; i++){
                 res[i] = run(graph, new HashMap<Object, Point>(positions), itr[i]);
             }
             return res;
        }
        public HashMap<Object, Point> runHelper(Graph<Object, ?> graph,HashMap<Object, Point> positions, int numIter){
            Collection<Object> vertices = graph.getVertices();
            HashMap<Object, Force> forces = new HashMap<Object, Force>();
             for ( int i = 0; i < numIter; i++){
                 for ( Object vertex : vertices ){
                     forces.put(vertex, calculateForces(graph, positions, vertex));
                 }
                 for ( Object vertex : vertices ){

                    positions.put(vertex, ForceFunctions.calculateShift(positions.get(vertex), forces.get(vertex)));
                 }
             }
             return positions;
        }
}

public class Eades extends Runnable {



public static Force calculateForces(Graph<Object, ?> graph, HashMap<Object, Point> positions, Object mainVertex){
    ArrayList<Object> vertices = new ArrayList<Object>(graph.getVertices());
    Force totalForce = new Force(0,0);
    Force neighborForce = new Force(0,0);
    Object neighborVertex = null;

    for ( int i = 0; i < vertices.size(); i++){
        neighborForce = null;
        neighborVertex = vertices.get(i);
        if ( mainVertex != neighborVertex ){  
            if ( graph.getNeighbors(mainVertex).contains(neighborVertex) ){
                neighborForce = ForceFunctions.attractive(positions.get(mainVertex),positions.get(neighborVertex));
            }
            else
                neighborForce = ForceFunctions.repulsive(positions.get(mainVertex), positions.get(neighborVertex) );

            totalForce.add(neighborForce);
        }
    }
    return totalForce;
}

但我收到一条错误消息:

The type Runnable cannot be the superclass of Eades; a superclass must be a class

我究竟做错了什么?

4

1 回答 1

2

Java包中有一个Runnable接口,java.lang和你的类冲突。编译器认为您想从Eades声明中的该接口进行扩展。

将您的类重命名为其他名称(MyRunnable或更有意义的名称),这将解决错误。

于 2013-08-24T15:51:53.137 回答