我一直在尝试以这种方式覆盖compareTo:这是原始的:
@Override
public int compareTo(ProductPart6 s)
{
return this.getproductName().compareTo(s.getproductName());
}
Run Code Online (Sandbox Code Playgroud)
这就是我试图重写它的方法:它抛出一个错误 SubClass 类型的方法compareTo(SubClass) 必须重写或实现超类型方法。
@Override
public int compareTo(SubClass s)
{
return this.getTitle().compareTo(s.getTitle());
}
Run Code Online (Sandbox Code Playgroud)
我认为这是非常错误的。我的 ProductPart6 没有 getTitle() ,这会导致它
@Override
public int compareTo(ProductPart6 s)
{
return this.getTitle().compareTo(s.getTitle());
}
Run Code Online (Sandbox Code Playgroud)
抛出错误(ProductPart6 类型的 getTitle() 未定义) - 如果我在那里定义了它,则没有必要覆盖它。我究竟做错了什么?我有扩展 ProductPart6 的子类,并且 ProductPart6 实现了 Comparable - 我以为我可以在子类上实现它,但没有。那是不行的。