Override注解报错怎么办 Method does not override method from its superclass

来源:4-4 编程练习

JackHe97

2021-03-25 11:48:37

http://img.mukewang.com/climg/605c081309243ed306370247.jpg

写回答

2回答

好帮手慕阿满

2021-03-25

同学你好,compareTo()方法的参数是Employee类型,表示要对Employee类型比较排序,所以在实现Comparable接口时,需要指明泛型是Employee。

http://img.mukewang.com/climg/605c25eb0975561603850209.jpg

如果不指明泛型<Employee>,重写的compareTo()方法参数应该是Object类型。

祝学习愉快~

0

JackHe97

提问者

2021-03-25

//实现Comparable接口
public class Employee implements Comparable {
//成员变量
private int no;
private String name;
private int salary;

//构造方法

public Employee() {
}

public Employee(int no, String name, int salary) {
this.no = no;
this.name = name;
this.salary = salary;
}

//getter和setter方法

public int getNo() {
return no;
}

public void setNo(int no) {
this.no = no;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getSalary() {
return salary;
}

public void setSalary(int salary) {
this.salary = salary;
}


//toString()方法

@Override
public String toString() {
return "no=" + no +
", name='" + name + '\'' +
", salary=" + salary +
'}';
}

@Override
public int compareTo(Employee o) {
int a = this.getSalary();
int b = o.getSalary();
int n = a - b;
return n;
}



0
hackHe97
hp>在接口后面加入泛型 就好了 这是为什么呢

public class Employee implements Comparable<Employee> {


h021-03-25
共1条回复

0 学习 · 16556 问题

查看课程