Java无法取消引用int

在使用Comparable借口,重写compareTo时出现无法取消int

img


出错地方

   @Override
   public int compareTo(Object o ){
     return this.quesuid.compareTo((Queshu) o).getQuesuid();
     }
package vo;

/**
 *
 * @author Administrator
 */
public class Queshu implements Comparable{
    private int quesuid ;//编号
    private String shu_name;//缺书书名
    private String banshe;//缺书出版社
    private String zuozhe;//作者
    private String chuban_date;//出版日期
    private String ISBN;//书号
    private String liuyan;//留言
    private String user;//用户名
    private String time;//登记时间

    public Queshu() {
    }

    public Queshu(int quesuid, String shu_name, String banshe, String zuozhe, String chuban_date, String ISBN, String liuyan, String user, String time) {
        this.quesuid = quesuid;
        this.shu_name = shu_name;
        this.banshe = banshe;
        this.zuozhe = zuozhe;
        this.chuban_date = chuban_date;
        this.ISBN = ISBN;
        this.liuyan = liuyan;
        this.user = user;
        this.time = time;
    }

    public int getQuesuid() {
        return quesuid;
    }

    public void setQuesuid(int quesuid) {
        this.quesuid = quesuid;
    }

    public String getShu_name() {
        return shu_name;
    }

    public void setShu_name(String shu_name) {
        this.shu_name = shu_name;
    }

    public String getBanshe() {
        return banshe;
    }

    public void setBanshe(String banshe) {
        this.banshe = banshe;
    }

    public String getZuozhe() {
        return zuozhe;
    }

    public void setZuozhe(String zuozhe) {
        this.zuozhe = zuozhe;
    }

    public String getChuban_date() {
        return chuban_date;
    }

    public void setChuban_date(String chuban_date) {
        this.chuban_date = chuban_date;
    }

    public String getISBN() {
        return ISBN;
    }

    public void setISBN(String ISBN) {
        this.ISBN = ISBN;
    }

    public String getLiuyan() {
        return liuyan;
    }

    public void setLiuyan(String liuyan) {
        this.liuyan = liuyan;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    @Override
    public String toString() {
        return "Queshu{" + "quesuid=" + quesuid + ", shu_name=" + shu_name + ", banshe=" + banshe + ", zuozhe=" + zuozhe + ", chuban_date=" + chuban_date + ", ISBN=" + ISBN + ", liuyan=" + liuyan + ", user=" + user + ", time=" + time + '}';
    }
     @Override 
   public int compareTo(Object o ){
     return this.quesuid.compareTo((Queshu) o).getQuesuid();
     }
}

成员变量 定义为 Integer 类型, compareTo 方法修改如下:

package other;

/**
 * @author huazie
 * @version 2.0.0
 * @since 2.0.0
 */

public class Queshu implements Comparable<Queshu>{
    private Integer quesuid ;//编号
    private String shu_name;//缺书书名
    private String banshe;//缺书出版社
    private String zuozhe;//作者
    private String chuban_date;//出版日期
    private String ISBN;//书号
    private String liuyan;//留言
    private String user;//用户名
    private String time;//登记时间

    public Queshu() {
    }

    public Queshu(int quesuid, String shu_name, String banshe, String zuozhe, String chuban_date, String ISBN, String liuyan, String user, String time) {
        this.quesuid = quesuid;
        this.shu_name = shu_name;
        this.banshe = banshe;
        this.zuozhe = zuozhe;
        this.chuban_date = chuban_date;
        this.ISBN = ISBN;
        this.liuyan = liuyan;
        this.user = user;
        this.time = time;
    }

    public int getQuesuid() {
        return quesuid;
    }

    public void setQuesuid(int quesuid) {
        this.quesuid = quesuid;
    }

    public String getShu_name() {
        return shu_name;
    }

    public void setShu_name(String shu_name) {
        this.shu_name = shu_name;
    }

    public String getBanshe() {
        return banshe;
    }

    public void setBanshe(String banshe) {
        this.banshe = banshe;
    }

    public String getZuozhe() {
        return zuozhe;
    }

    public void setZuozhe(String zuozhe) {
        this.zuozhe = zuozhe;
    }

    public String getChuban_date() {
        return chuban_date;
    }

    public void setChuban_date(String chuban_date) {
        this.chuban_date = chuban_date;
    }

    public String getISBN() {
        return ISBN;
    }

    public void setISBN(String ISBN) {
        this.ISBN = ISBN;
    }

    public String getLiuyan() {
        return liuyan;
    }

    public void setLiuyan(String liuyan) {
        this.liuyan = liuyan;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    @Override
    public String toString() {
        return "Queshu{" + "quesuid=" + quesuid + ", shu_name=" + shu_name + ", banshe=" + banshe + ", zuozhe=" + zuozhe + ", chuban_date=" + chuban_date + ", ISBN=" + ISBN + ", liuyan=" + liuyan + ", user=" + user + ", time=" + time + '}';
    }
    @Override
    public int compareTo(Queshu o ){
        return this.quesuid.compareTo(o.getQuesuid());
    }
}


在你的代码中,quesuid 属性的类型应该是一个对象类型,而不是一个基本类型。因为 compareTo 方法的参数类型是 Object,所以当你将它转换为 Queshu 对象时,它将成为一个对象类型,而不是一个基本类型。因此,你应该使用对象类型的 compareTo 方法,而不是基本类型的 compareTo 方法。修改后的代码如下所示:

@Override
public int compareTo(Object o) {
    Queshu other = (Queshu) o;
    return this.quesuid.compareTo(other.quesuid);
}

在这个版本的 compareTo 方法中,我们首先将 o 强制转换为 Queshu 对象,然后使用 quesuid 属性的 compareTo 方法来比较两个对象。由于 quesuid 属性的类型是一个对象类型,所以我们可以直接使用它的 compareTo 方法,而不需要使用 getQuesuid 方法。

另外,你还需要确保 quesuid 属性实现了 Comparable 接口,否则它的 compareTo 方法将无法使用。

这个问题简单,在你的代码中,quesuid 是一个 int 类型的变量,而 compareTo 方法需要返回一个 int 类型的值,因此不能直接调用 compareTo 方法来比较 quesuid 。需要将 quesuid 转换为 Integer 对象,然后调用 compareTo 方法来比较它们。修改 compareTo 方法如下

@Override 
public int compareTo(Object o) {
    Queshu other = (Queshu) o;
    return Integer.compare(this.quesuid, other.getQuesuid());
}

首先将 o 转换为 Queshu 对象,然后比较它们的 quesuid 属性。 使用 Integer.compare 方法来比较它们的值,这样可以更方便地进行比较。