当前位置: 首页 > 知识库问答 >
问题:

类不是抽象的,并且不重写carable[closed]中的抽象方法compareTo(X)

冀望
2023-03-14

我试图使用carable接口以dd-mm-yyyy hh:mm格式比较约会时间(字符串)。而且,它似乎找不到getAppointmentTime方法,它显然在那里

HW3\Appointment.java:13: error: Appointment is not abstract and does not override abstract method compareTo(Appointment) in Comparable
public class Appointment implements Comparable<Appointment>
       ^
HW3\Appointment.java:189: error: name clash: compareTo(Object) in Appointment and compareTo(Appointment) in Comparable have the same erasure, yet neither overrides the other
    public int compareTo(Object obj)
               ^
HW3\Appointment.java:191: error: cannot find symbol
        return getAppointmentTime().compareTo(obj.getAppointmentTime());
                                                 ^
  symbol:   method getAppointmentTime()
  location: variable obj of type Object

代码:

package HW3;

import java.lang.Comparable;

public class Appointment implements Comparable<Appointment>
{

private String serviceType;
private int patientID;
private String patientName;
private String appointmentTime;
private String doctorName;
private String symptoms;

public Appointment(String serviceType, int patientID, String patientName, String appointmentTime, String doctorName, String symptoms)
{
this.serviceType = serviceType;
this.patientID = patientID;
this.patientName = patientName;
this.appointmentTime = appointmentTime;
this.doctorName = doctorName;
this.symptoms = symptoms;
}

public String getServiceType()
{
  return serviceType;
}


public int getPatientID()
{
  return patientID;
}


public String getPatientName()
{
  return patientName;
}


public String getAppointmentTime()
{
  return appointmentTime;
}


public String getDoctorName()
{
  return doctorName;
}

public String getSymptoms()
{
  return symptoms;
}

public int compareTo(Object obj)
{
    return getAppointmentTime().compareTo(obj.getAppointmentTime());
}

}

共有1个答案

秦俊发
2023-03-14

尝试下面的代码:

package HW3;

import java.lang.Comparable;

public class Appointment implements Comparable<Appointment> {

private String serviceType;
private int patientID;
private String patientName;
private String appointmentTime;
private String doctorName;
private String symptoms;

public Appointment(String serviceType, int patientID, String patientName, String appointmentTime, String doctorName, String symptoms) {
    this.serviceType = serviceType;
    this.patientID = patientID;
    this.patientName = patientName;
    this.appointmentTime = appointmentTime;
    this.doctorName = doctorName;
    this.symptoms = symptoms;
}

public String getServiceType() {
    return serviceType;
}


public int getPatientID() {
    return patientID;
}


public String getPatientName() {
    return patientName;
}


public String getAppointmentTime() {
    return appointmentTime;
}


public String getDoctorName() {
    return doctorName;
}

public String getSymptoms() {
    return symptoms;
}

@Override
public int compareTo(Appointment obj) {
    return getAppointmentTime().compareTo(obj.getAppointmentTime());
 }
}

更改:将compareto方法的方法参数从Object更改为Apption

 类似资料: