方法重写不起作用
Method overriding not working
好的。我有一个标准的合并排序 class.
public class Merge extends SortAlgorithm {
public static void sort(Comparable[] a) {
Comparable[] aux = new Comparable[a.length];
sort(a, aux, 0, a.length -1);
}
protected static void sort(Comparable[] a, Comparable[] aux, int lo, int hi) {
if(hi <= lo)
return;
int mid = (lo + hi)/2;
sort(a, aux, lo, mid);
sort(a, aux, mid+1, hi);
if(less(a[mid+1], a[mid]))
merge(a, aux, lo, mid, hi);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] a = br.readLine().split(" ");
sort(a);
show(a);
}
protected static void merge(Comparable[] a, Comparable[] aux, int lo, int mid, int hi) {
int i = lo , j = mid+1;
for(int k = lo ; k <= hi ; k++)
aux[k] = a[k];
for( int k = lo ; k <= hi ; k++ ) {
if(i > mid) //Boundary Conditions
a[k] = aux[j++]; //
else if(j > hi) //
a[k] = aux[i++]; //
else if(less(aux[i] , aux[j]))
a[k] = aux[i++];
else
a[k] = aux[j++];
}
}
}
我已扩展此 class 以创建使用较少辅助数组的更新版本的合并排序。它具有不同的 'merge' 和 'sort(Comparable[])' 功能
public class MergeSmallerAuxArray extends Merge {
public static void sort(Comparable[] a) {
Comparable[] aux = new Comparable[a.length/2];
sort(a, aux, 0, a.length - 1);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] a = br.readLine().split(" ");
sort(a);
show(a);
}
//method overidden below
public static void merge(Comparable[] a, Comparable[] aux, int lo, int mid, int hi) { int i = lo , j = mid+1;
for(int k = lo ; k <= mid ; k++)
aux[k] = a[k];
for(int k = lo ; k <= hi ; k++) {
if(i > mid)
a[k] = a[j++];
else if(j > hi)
a[k] = aux[i++];
else if(less(aux[i], a[j]))
a[k] = aux[i++];
else
a[k] = a[j++];
}
}
}
但是每次我 运行 这个 class 我都会得到一个错误,因为 superclass 的 sort(Comparable[],Comparable[],int,int)
方法正在调用 superclass 而不是我覆盖的那个。我哪里出错了?
请帮忙。
谢谢你。
这里的问题是您使用了 static
关键字。 static
不适用于多态性。
好的。我有一个标准的合并排序 class.
public class Merge extends SortAlgorithm {
public static void sort(Comparable[] a) {
Comparable[] aux = new Comparable[a.length];
sort(a, aux, 0, a.length -1);
}
protected static void sort(Comparable[] a, Comparable[] aux, int lo, int hi) {
if(hi <= lo)
return;
int mid = (lo + hi)/2;
sort(a, aux, lo, mid);
sort(a, aux, mid+1, hi);
if(less(a[mid+1], a[mid]))
merge(a, aux, lo, mid, hi);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] a = br.readLine().split(" ");
sort(a);
show(a);
}
protected static void merge(Comparable[] a, Comparable[] aux, int lo, int mid, int hi) {
int i = lo , j = mid+1;
for(int k = lo ; k <= hi ; k++)
aux[k] = a[k];
for( int k = lo ; k <= hi ; k++ ) {
if(i > mid) //Boundary Conditions
a[k] = aux[j++]; //
else if(j > hi) //
a[k] = aux[i++]; //
else if(less(aux[i] , aux[j]))
a[k] = aux[i++];
else
a[k] = aux[j++];
}
}
}
我已扩展此 class 以创建使用较少辅助数组的更新版本的合并排序。它具有不同的 'merge' 和 'sort(Comparable[])' 功能
public class MergeSmallerAuxArray extends Merge {
public static void sort(Comparable[] a) {
Comparable[] aux = new Comparable[a.length/2];
sort(a, aux, 0, a.length - 1);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] a = br.readLine().split(" ");
sort(a);
show(a);
}
//method overidden below
public static void merge(Comparable[] a, Comparable[] aux, int lo, int mid, int hi) { int i = lo , j = mid+1;
for(int k = lo ; k <= mid ; k++)
aux[k] = a[k];
for(int k = lo ; k <= hi ; k++) {
if(i > mid)
a[k] = a[j++];
else if(j > hi)
a[k] = aux[i++];
else if(less(aux[i], a[j]))
a[k] = aux[i++];
else
a[k] = a[j++];
}
}
}
但是每次我 运行 这个 class 我都会得到一个错误,因为 superclass 的 sort(Comparable[],Comparable[],int,int)
方法正在调用 superclass 而不是我覆盖的那个。我哪里出错了?
请帮忙。
谢谢你。
这里的问题是您使用了 static
关键字。 static
不适用于多态性。