继承基于文本的游戏的类
我正在尝试为课堂创建一个基于文本的游戏,但我一直在尝试让我的主类 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法创建抽象类的实例
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 inheritsArtifact
and call the constructor as thisArtifact artifact=new MyNewArtifact();
您无法创建抽象变量的实例。因此,
AbstractClass ac=new AbstractClass()
将引发编译时错误。相反,您需要另一个类来从抽象类继承。
例如:
然后使用:
最后创建:
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:
Then use:
Finally create with:
我会这样做
像这样使用它:
I'd do this
Use it like this: