我是Java新手。我有一个继承问题。 我试着写一些排序类。他们也有相同的数据结构和相同的方法。 所以我尝试使用继承。 我写了这个基类。
public class Sort {
private int[] lst;
public Sort(int[] lst) {
this.lst = lst;
}
public void sort() {
return;
}
public String toString() {
String aa = "";
for (int innt : this.lst) {
aa = aa + innt + "" + " ";
}
return aa;
}
}
和这个
public class Select extends Sort{
private int[] lst;
public Select(int[] lst) {
super(lst);
}
public void sort() {
for (int i = 0; i < this.lst.length; i++) {
int small = lst[i];
int small_ind = i;
for (int j = i + 1; j < this.lst.length; j++) {
if (lst[j] < small) {
small = lst[j];
small_ind = j;
}
}
int temp = lst[i];
lst[i] = small;
lst[small_ind] = temp;
}
}
}
但是当我运行这个
时public class Test {
public static void main(String[] args) {
int[] a = {5, 6, 7, 5, 7, 3, 2, 1, 4, 8};
Sort sl = new Select(a);
sl.sort();
System.out.println(sl.toString());
}
}
它不起作用。线程“main”java.lang.NullPointerException中的异常 它只在我将toString方法移动到Select类时才有效,并删除“extends Sort”。 你介意帮帮我吗?感谢。
答案 0 :(得分:2)
如果您移动toString()
,它就不起作用,因为当您致电NullPointerException
而不是sort()
时,toString()
似乎正在发生,因为在该行中: -
for (int i = 0; i < this.lst.length; i++) {
您正在尝试访问已创建的成员(lst
)的length
,仅显示其父级私有版本。
你要么要摆脱孩子班级&#39; lst
并将父母版本protected
设为private
: -
protected int[] lst; //In Sort class
或在其构建器中设置子项lst
: -
public Select(int[] lst) { //In Select class
this.lst = lst;
}
答案 1 :(得分:0)
因为你有不同的名单!在任何实施中。他们每个人都是私人的。
这应该有效:
public abstract class Sort {
protected int[] lst;
public Sort(int[] lst) {
this.lst = lst;
}
public abstract void sort();
public String toString() {
String aa = "";
for (int innt : this.lst) {
aa = aa + innt + "" + " ";
}
return aa;
}
}
现在,列表在任何实现中都受到保护和访问。此类是抽象的以预先直接使用,您必须立即实现。 sort方法也是抽象的,以确保实现正在实现它;)
这里的实施:
public class Select extends Sort{
public Select(int[] lst) {
super(lst);
}
public void sort() {
for (int i = 0; i < this.lst.length; i++) {
int small = lst[i];
int small_ind = i;
for (int j = i + 1; j < this.lst.length; j++) {
if (lst[j] < small) {
small = lst[j];
small_ind = j;
}
}
int temp = lst[i];
lst[i] = small;
lst[small_ind] = temp;
}
}
}
答案 2 :(得分:0)
我已经测试了你的代码。我已经更改了代码中的小东西并且运行正常。我不知道这是否有帮助。
它对我有用。你可以尝试一下。
整个代码是,
在Sort类中,
package testing;
public class Sort {
private int[] lst;
public Sort(int[] lst) {
this.lst = lst;
}
public void sort(int[] a) {
return;
}
public String toString() {
String aa = "";
for (int innt : this.lst) {
aa = aa + innt + "" + " ";
}
return aa;
}
}
在选择课程中
package testing;
public class Select extends Sort{
private int[] lst;
public Select(int[] lst) {
super(lst);
}
public void sort(int[] lst) {
for (int i = 0; i <lst.length; i++) {
int small = lst[i];
int small_ind = i;
for (int j = i + 1; j <lst.length; j++) {
if (lst[j] < small) {
small = lst[j];
small_ind = j;
}
}
int temp = lst[i];
lst[i] = small;
lst[small_ind] = temp;
}
}
}
并且在测试类中,
package testing;
public class Test{
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = {5, 6, 7, 5, 7, 3, 2, 1, 4, 8};
Sort sl = new Select(a);
sl.sort(a);
//sl.sort();
System.out.println(sl.toString());
}
}