gpt4 book ai didi

java - 抽象 compareTo 方法未被覆盖

转载 作者:搜寻专家 更新时间:2023-11-01 00:59:46 24 4
gpt4 key购买 nike

编译下面的代码时,出现以下错误:

PersonalInformation is not abstract and does not override abstract method compareTo(Object) in Comparable

我认为这意味着我的 compareTo 方法有问题。但一切似乎都还好。有人有什么建议吗?

import java.util.*;
public class PersonalInformation implements Comparable
{
private String givenName;
private String middleInitial;
private String surname;
private String gender;
private String emailAddress;
private String nationalId;
private String telephoneNum;
private String birthday;

public PersonalInformation(String gN, String mI,
String sur, String gen, String eMa, String natId,
String teleNum, String birthd)

{
givenName = gN;
middleInitial = mI;
surname = sur;
gender = gen;
emailAddress = eMa;
nationalId = natId;
telephoneNum = teleNum;
birthday = birthd;


}


public int compareTo(PersonalInformation pi)
{
return (this.gender).compareTo(pi.gender);
}

}

最佳答案

这样做:

public int compareTo(Object pi) {
return ((PersonalInformation )(this.gender)).compareTo(((PersonalInformation ) pi).gender);
}

或更好

public class PersonalInformation implements Comparable<PersonalInformation>

如果您实现Comparable 接口(interface),则必须使用第一种方法为所有对象实现它,或者以第二种方式键入您的类。

关于java - 抽象 compareTo 方法未被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9362642/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com