简体   繁体   English

继承基于文本的游戏的类

[英]Inheriting classes for a text-based game

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. 我正在尝试为课堂创建一个基于文本的游戏,而我一直试图让我的主课GCPUAPP从Artifact课中读取。

Here's the code i've inputed for the GCPUAPP class: 这是我为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();

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. 我是Java的新手,所以我完全陷入困境。

You can't create an instance of an abstract class Artifact artifact=new Artifact(); 您无法创建抽象类的实例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(); 从您的类定义中删除abstract符号,或者创建另一个继承Artifact类,并以该Artifact artifact=new MyNewArtifact();调用构造函数Artifact artifact=new MyNewArtifact();

You can't create an instance of an abstract variable. 您无法创建抽象变量的实例。 So, AbstractClass ac=new AbstractClass() would throw a compile-time error. 因此, AbstractClass ac=new AbstractClass()将引发编译时错误。 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();

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM