2013-08-24 133 views
0

我下面从here的例子:Java继承错误

在接口上的部分,有人指出,实现接口必须实现所有的接口方法的类。但是,可以定义一个不实现所有接口方法的类,只要该类声明为抽象类。例如,

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 

我在做什么错?

回答

2

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

将你的班级重新命名为其他东西(MyRunnable或其他更有意义的东西),这将处理错误。

+2

不,它在'java.lang'中有'Runnable' - 这与“默认包”不一样。 –

+0

@JonSkeet:是的,编辑我的答案。感谢您指出了这一点。 –

+0

@SayemAhmed谢谢我从来没有想过它。 – mihajlv