admin管理员组

文章数量:1636810

The type Student must implement the inherited abstract method ComparablepareTo(Object)

重写compareTo()方法报错

参考链接: 实现 Comparable:“必须实现继承的抽象方法”错误.

public class Student implements Comparable {
//报错:The type Student must implement the inherited abstract method ComparablepareTo(Object)
//改为泛型:public class Student implements Comparable<Student> {顺利解决

	private String stu_name;
	private int stu_id;
	private int stu_age;
	
	public Student(String name,int id,int age) {
		this.stu_name = name;
		this.stu_id = id;
		this.stu_age = age;
	}

	//getter and setter methods
	
	@Override
	public int compareTo(Student stu) {
		int stu_id = ((Student)stu).getStu_id();
		return this.stu_id - stu_id;
	}
	
	@Override
	public String toString() {
		return "[stu_name =" + stu_name + "stu_id = " + stu_id + "stu_age = " + stu_age + "]";
	}
	
}

本文标签: 重写报错implementtypeinherited