【发布时间】:2012-10-24 14:42:00
【问题描述】:
我遵循了一个教程,但未能为我的BST 制作我的Country 类Comparable。
主要:
BinarySearchTree A = new BinarySearchTree();
Country a = new Country("Romania", "Bucharest", 1112);
A.insert(a);
国家级:
public int compareTo(Object anotherCountry) throws ClassCastException {
if (!(anotherCountry instanceof Country))
throw new ClassCastException("A Country object expected.");
String anotherCountryName = ((Country) anotherCountry).getName();
int i = this.name.compareTo(anotherCountryName);
if(i < 0){
return -1;
} else {
return 0;
}
}
错误:
@Override
public int compareTo(Object anotherCountry) throws ClassCastException {
if (!(anotherCountry instanceof Country))
throw new ClassCastException("A Country object expected.");
String anotherCountryName = ((Country) anotherCountry).getName();
return this.name.compareTo(anotherCountryName);
Description Resource Path Location Type
名称冲突:Country 类型的方法 compareTo(Object) 与 Comparable 类型的 compareTo(T) 具有相同的擦除,但不会覆盖 Country.java /Lab2_prob 4/src 第 17 行 Java 问题
Description Resource Path Location Type
The method compareTo(Object) of type Country must override or implement a supertype method Country.java /Lab2_prob 4/src line 17 Java Problem
和类:
public class Country implements Comparable<Country>{
private String name;
private String capital;
private int area;
Description Resource Path Location Type
类型Country必须实现继承的抽象方法Comparable.compareTo(Country) Country.java /Lab2_prob 4/src line 2 Java Problem
【问题讨论】:
-
您的
Country课程是否扩展Comparable<Country>? -
您实际上遇到了什么问题或错误?
标签: java comparable