【问题标题】:Importing overridden interface methods [duplicate]导入重写的接口方法[重复]
【发布时间】:2015-11-16 23:27:21
【问题描述】:

我有两个类和一个接口。我在接口中创建了方法,并在实现接口的类中覆盖了它们。现在我正在尝试将这些方法导入另一个类。由于某种原因,他们的方法没有找到。非常沮丧,因为我不知道为什么。接口、重写类和导入方法的类按确切顺序如下。为什么不能导入?:

public interface SortInterface {

    public abstract void recursiveSort(int[] list);

    public abstract void iterativeSort(int[] list);

    int getCount();

    long getTime();

}

覆盖类:

public class YourSort implements SortInterface{
      @Override
    public void iterativeSort(int[] list) {
        for(int i =1; i< list.length; i++){
        int temp = list[i];
        int j;
        for (j = i-1; j>=0 && temp < list[j]; j--)
            list[j+1] = list[j];
        list[j+1] = temp;   
    }}

    public static void recursiveSort(int array[], int n, int j) {
if (j < n) {
    int i;
    int temp = array[j];

    for (i=j; i > 0 && array[i-1] > temp; i--) array[i] = array[i-1];
    array[i] = temp;

    recursiveSort(array,n, j+1);  }} 


    @Override
    public void recursiveSort(int[] list) {
        int j = list.length;
         int n=0;
         if (j < n) {
        int i;
        int temp = list[j];

        for (i=j; i > 0 && list[i-1] > temp; i--) list[i] = list[i-1];
        list[i] = temp;

        recursiveSort(list,n, j+1);  }
    } 



    @Override
    public int getCount() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public long getTime() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

导入类:

public class SortMain {
 static int[] b;
static int[] c;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        b = new int[5];
        b[0]= 8;
        b[1]=5;
        b[2]=9;
        b[3]=4;
        b[4]=2;
        recursiveSort(b);

        c = new int[5];
        c[0]= 8;
        c[1]=5;
        c[2]=9;
        c[3]=4;
        c[4]=2;
        recursiveSort(b);

        for(int i = 0; i<5; i++){
            System.out.println(b[i]);
        }

        iterativeSort(c);
         System.out.println("");

        for(int i = 0; i<5; i++){
            System.out.println(c[i]);
        }
    }
}

【问题讨论】:

  • 你觉得recursiveSort(b);应该怎么做?你为什么这么认为?
  • recursiveSort(b) 是一种实现递归排序的方法。
  • 不,我不是这个意思。你认为 表达式 应该做什么,你为什么这么认为?是方法调用吗?静态方法还是实例方法?如果是静态方法,它属于哪个类?编译器是怎么知道的?如果是实例方法,是哪个实例?编译器是怎么知道的?
  • 因为这似乎是家庭作业,并且没有明显的理由使用界面,我假设您已被告知使用界面。如果没有,您应该在实用程序类上有一个静态方法(请参阅我的答案)
  • 有人告诉我使用接口。上面发布的 YourSort 类实现了该类。 YourSort 还实现了该方法的接口。

标签: java class import interface


【解决方案1】:

接口上的所有方法都是public abstract 隐式的。在 Java 8 之前,您可以使用方法做任何其他事情。

接口上的所有方法(Java 8 之前)都是您编写的 instance 方法。这意味着它们必须在接口的实例上工作。

注意:按照您编写方法的方式,它们实际上并不需要实例,可能根本就应该在 interface 上。您可以将它们设为static 方法,但更好的选择是将它们移至实用程序类,然后将它们从SortMain 移到import static 那里。

您可以编写最少的更改

new YourSort().recursiveSort(b);

虽然这会起作用,但由于多种原因,这很糟糕,尤其是因为 YourSort 对象毫无意义,只需要让代码编译即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    • 1970-01-01
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多