我跟着一個教程,但未能爲我的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
名稱衝突:類型國家的方法的compareTo(對象)具有相同的擦除作爲的compareTo (T)類型Comparable但不覆蓋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
類型國家必須實現繼承抽象方法Comparable.compareTo(國家或地區)Country.java/Lab2_prob 4/SRC 2行的Java問題
您的'Country'類是否擴展了'Comparable'? –
你真的得到了什麼問題或錯誤? – DNA
抱歉只是添加了錯誤。 –