2013-08-24 137 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