【发布时间】: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