继承基于文本的游戏的类

发布于 2024-10-18 12:04:35 字数 548 浏览 2 评论 0原文

我正在尝试为课堂创建一个基于文本的游戏,但我一直在尝试让我的主类 GCPUAPP 从我的 Artifact 类中读取内容。

这是我为 GCPUAPP 类输入的代码:

Artifact artifact=new Artifact();
artifact.name="Harry Potter and the Deathly Hallows";
artifact.description="Harry and his friends save the qizarding world again";
r1.contents=artifact;
dialog();

它给了我一个关于“new Artifact”的错误。这是我在 Artifact 上的代码:

public abstract class Artifact{ 

    String name, description;

    public String toString(){
        return name;
}

我是 Java 新手,所以我完全陷入困境。

I'm trying to create a text-based game for class and i'm stuck on trying to get my main class, GCPUAPP, to read from my Artifact class.

Here's the code i've inputed for the GCPUAPP class:

Artifact artifact=new Artifact();
artifact.name="Harry Potter and the Deathly Hallows";
artifact.description="Harry and his friends save the qizarding world again";
r1.contents=artifact;
dialog();

It's giving me an error on "new Artifact". Here's the code I have on Artifact:

public abstract class Artifact{ 

    String name, description;

    public String toString(){
        return name;
}

I'm new to Java so I am completely stuck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遗忘曾经 2024-10-25 12:04:35

您无法创建抽象类的实例 Artifact artifact=new Artifact();

这就是抽象类的要点。只有继承抽象类的非抽象类才能实例化为对象。

从类定义中删除 abstract 表示法,或者创建另一个继承 Artifact 的类并调用构造函数,如下 Artifact artifact=new MyNewArtifact();

You can't create an instance of an abstract class Artifact artifact=new Artifact();

That is the point of abstract classes. Only non-abstract classes that inherit the abstract class can be instanced as object.

Either remove the abstract notation from your class definition, or create another class that inherits Artifact and call the constructor as this Artifact artifact=new MyNewArtifact();

盗梦空间 2024-10-25 12:04:35

您无法创建抽象变量的实例。因此,AbstractClass ac=new AbstractClass() 将引发编译时错误。
相反,您需要另一个类来从抽象类继承。
例如:

public abstract class AbstractClassArtifact{ 

    String name, description;

    public String toString(){
        return name;
}

然后使用:

 public class Artifact extends AbstractClassArtifact{
   public Artifact(String name, String description){ //Constructor to make setting variables easier
     this.name=name;
     this.description=description;
   }
 }

最后创建:

 Artifact artifact=new Artifact("Harry Potter and the Deathly Hallows", "Harry and his friends save the qizarding world again");
 r1.contents=artifact.toString();
 dialog();

You can't create an instance of an abstract variable. So, AbstractClass ac=new AbstractClass() would throw a compile-time error.
Instead, you need another class to inherit from the abstract class.
For example:

public abstract class AbstractClassArtifact{ 

    String name, description;

    public String toString(){
        return name;
}

Then use:

 public class Artifact extends AbstractClassArtifact{
   public Artifact(String name, String description){ //Constructor to make setting variables easier
     this.name=name;
     this.description=description;
   }
 }

Finally create with:

 Artifact artifact=new Artifact("Harry Potter and the Deathly Hallows", "Harry and his friends save the qizarding world again");
 r1.contents=artifact.toString();
 dialog();
终陌 2024-10-25 12:04:35

我会这样做

class HarryPotterArtifact extends Artifact {

    // no need to declare name and desc, they're inherited by "extends Artifact"

    public HarrayPotterArtifact(String name, String desc) {
         this.name = name;
         this.desc = desc;
    }
}

像这样使用它:

//Artifact artifact=new Artifact();
//artifact.name="Harry Potter and the Deathly Hallows";
//artifact.description="Harry and his friends save the qizarding world again";

  String harryName = "Harry Potter and the Deathly Hallows";
  String harryDesc = "Harry and his friends save the qizarding world again";
  Artifact artifact = new HarryPotterArtifact(harryName,harryDesc);

I'd do this

class HarryPotterArtifact extends Artifact {

    // no need to declare name and desc, they're inherited by "extends Artifact"

    public HarrayPotterArtifact(String name, String desc) {
         this.name = name;
         this.desc = desc;
    }
}

Use it like this:

//Artifact artifact=new Artifact();
//artifact.name="Harry Potter and the Deathly Hallows";
//artifact.description="Harry and his friends save the qizarding world again";

  String harryName = "Harry Potter and the Deathly Hallows";
  String harryDesc = "Harry and his friends save the qizarding world again";
  Artifact artifact = new HarryPotterArtifact(harryName,harryDesc);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文