gpt4 book ai didi

java - 缺少方法主体,或声明抽象

转载 作者:行者123 更新时间:2023-12-02 04:50:24 27 4
gpt4 key购买 nike

我收到此错误消息:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: Pet.saySomething at PetTest.main(PetTest.java:18) Java Result: 1

这是我所拥有的:对于Speak类,

public class Speak {
public void saySomething();
}

这是我在 PetTest 类中的内容:

 import java.util.ArrayList;

public class PetTest {

public static void main(String[] args)
{
ArrayList<Pet> myPets = new ArrayList<Pet>();

myPets.add(new Parrot("Boss"));
myPets.add(new Cat("Oreo"));
myPets.add(new Dog("Riley"));

for (Pet p: myPets)
{
p.saySomething();
System.out.println(p);
}
}
}

我没有发布与该程序相关的其他 5 门类(class)。我知道 Speak 类中需要修复一些问题,但我被困住了。在 Speak 类中,在 public void saySomething(); 行中,它表示缺少方法主体,或声明抽象。

编辑:以下是其他类:

 public abstract class Pet {

String name;

public Pet(String name){
this.name = name;
}
}

public class Parrot extends Bird {

public Parrot (String name)
{
super(name);
}

@Override
public void saySomething()
{
System.out.println("Pauly want a craker?");
}

@Override
public String toString()
{
return "Hello, I'm " + name + " the parrot.";
}

}


public class Dog extends Pet {

public Dog (String name)
{
super(name);
}

@Override
public void saySomething()
{
System.out.println("Ruff, ruff!");
}

@Override
public String toString()
{
return "Hello, I'm " + name + " the dog.";
}
}


public class Cat extends Pet {

public Cat(String name)
{
super(name);
}

@Override
public void saySomething()
{
System.out.println("Meow.");
}

@Override
public String toString()
{
return "Hello, I'm " + name + " the cat.";
}
}

public class Bird extends Pet {

public Bird(String name)
{
super(name);
}

public void saySomething(){}

}

最佳答案

您有以下发言选项,请选择:

第一

public abstract class Speak{

public abstract void saySomething();

}

第二

public class Speak{

public void saySomething(){

}
}

第三

public interface Speak{

public void saySomething();
}

编辑:首先,您没有在任何地方使用 Speak 类。其次,请在Pet类中添加以下方法:

 public abstract void saySomething();

关于java - 缺少方法主体,或声明抽象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29293427/

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