Interface “我该如何解决?”;{expected error";并且能够继续此挑战吗?
Noob到java。无法使接口正常工作。在我的接口“{expected”中获取错误消息,但由于存在错误消息,因此不确定如何进行补救 主文件:Interface “我该如何解决?”;{expected error";并且能够继续此挑战吗?,interface,comparable,treeset,Interface,Comparable,Treeset,Noob到java。无法使接口正常工作。在我的接口“{expected”中获取错误消息,但由于存在错误消息,因此不确定如何进行补救 主文件: import java.util.Iterator; import java.util.TreeSet; import javax.swing.JOptionPane; public class hw2b { public static void main(String[] args) { TreeSet&l
import java.util.Iterator;
import java.util.TreeSet;
import javax.swing.JOptionPane;
public class hw2b
{
public static void main(String[] args)
{
TreeSet<Pet> pets = new TreeSet<Pet>();
pets.add(new Cat("Calico", "5"));
pets.add(new Cat("Siamese", "10"));
pets.add(new Dog("Irish Wolfhound", "5"));
pets.add(new Dog("Border Collie", "10"));
String choice;
choice = JOptionPane.showInputDialog("Enter your choice\n" +
"Quit\n" +
"Print\n" +
"Add Cat\n" +
"Add Dog");
while (! choice.equalsIgnoreCase("Quit"))
{
if (choice.equalsIgnoreCase("Print"))
{
print(pets); // List will be output in alphabetical order by breed name
}
else if (choice.equalsIgnoreCase("Add Cat"))
{
String breed = JOptionPane.showInputDialog("Enter breed of cat");
String years = JOptionPane.showInputDialog("Enter age of cat");
pets.add(new Cat(breed, years));
System.out.println(breed + years + " added");
}
else if (choice.equalsIgnoreCase("Add Dog"))
{
String breed = JOptionPane.showInputDialog("Enter breed of dog");
String years = JOptionPane.showInputDialog("Enter age of dog");
pets.add(new Dog(breed, years));
System.out.println(breed + years + " added");
}
choice = JOptionPane.showInputDialog("Enter your choice\n" +
"Quit\n" +
"Print\n" +
"Add Cat\n" +
"Add Dog");
}
}
static private void print(TreeSet data)
{
Iterator li = data.iterator();
System.out.println();
System.out.println();
while (li.hasNext())
{
System.out.println(li.next());
}
}
}
实现我的接口的类:
public class Cat implements Pet // Dog would be nearly identical to this
{
private String breed;
private String years;
Cat(String breed, String years)
{
this.breed = breed;
this.years = years;
}
public String toString()
{
return "Pet's breed is " + getBreed() +
"\nPet's age is " + getAge() +
"\n";
}
public String getBreed()
{
return breed;
}
public double getAge()
{
double age;
if (years == "1")
{
age = 15;
}
else if (years == "2")
{
age = 24;
}
else
{
int yrs;
yrs = Integer.parseInt(years);
age = ((yrs - 2) * 4) + 24;
}
return age;
}
}
我的比较国:
import java.util.Comparator;
public class PetComparator implements Comparator
{
public int compare(Object a, Object b)
{
Pet p1 = (Pet) a;
Pet p2 = (Pet) b;
String p1Breed = p1.getBreed();
String p2Breed = p2.getBreed();
return p1Breed.compareToIgnoreCase(p2Breed);
}
}
我们的目标是能够从树集开始,将猫和/或狗的品种和年龄信息添加到列表中,然后按品种名称排序输出列表。一旦我克服了这个错误,我确信我可以解决其余的挑战,但任何洞察或指点都将是最受欢迎的。请参阅界面代码顶部的注释,以了解这些信息的位置ror消息说错误是。请提前感谢。接口不实现任何东西。类实现任何东西
编译器不会在
接口
声明后将实现
识别为有效关键字,因此(在没有扩展
关键字的情况下)它需要一个分号,但没有得到分号。一个接口不能实现另一个接口。一个接口只指定一个约定,因此让一个接口“实现”某个东西是没有意义的
改用扩展
:
public interface Pet extends Comparable {
...
}
此外,没有必要将接口中的方法标记为
public
,因为所有接口方法都是隐式public
现在我在编译时收到一条消息,说“线程中的异常”main“java.lang.AbstractMethodError:Cat.comparTo(Ljava/lang/Object;)I”
public interface Pet extends Comparable {
...
}