我正在尝试编写一个小项目,并且对类的概念仍然很陌生,并且我的编程更加面向对象。
我已经创建了一个类,但是当我尝试在不同的类中实现它的变量时,它不起作用。我收到的错误是“缺少方法体”。
我在访问方法/类时也遇到了问题 - 由于某种原因我无法在 Main 中初始化我的类。我不断收到一条消息,要求将变量更改为静态(尽管我被建议不要这样做)。
我希望这是有道理的。任何帮助将不胜感激,谢谢!
这是我的班级门
` class Door {
String description;
int damage;
String keyItem;
boolean checkItem(String item) {
if (Objects.equals(keyItem, "") || Objects.equals(item, keyItem)) {
return true;
}
return false;
}
public Door(String description, int damage, String keyItem, boolean checkItem){
}
}`
这是我尝试实现到另一个类中的时候
`class HardRoom extends Room {
public HardRoom() {
}
Door[] doors = new Door[4];
doors[0] = new Door("A wooden red door, it stares at you intently. You notice a few chips engraved onto the door. What kind of damage have people tried inflicting on this door..?", -2, "strawberry", false); `
这是我的 Main 方法,如果不将它们全部静态化,我实际上无法创建新的 Room、HardRoom、HarderRoom 或 Player。
` public static void main(String args[]) {
while (true) {
Room entrance = new Room();
HardRoom doorRoom = new HardRoom();
HarderRoom keyRoom = new HarderRoom();
Player newPlayer = new Player();
newPlayer.init();
`
如果我需要显示任何其他代码片段,请告诉我,谢谢。
'...我已经创建了一个类,但是当我尝试在不同的类中实现它的变量时,它不起作用。我收到的错误是“缺少方法体”。 ...'
您只能在类范围中声明成员。
class HardRoom extends Room {
Door[] doors = new Door[4];
public HardRoom() {
doors[0] = new Door("A wooden red door, it stares at you intently. You notice a few chips engraved onto the door. What kind of damage have people tried inflicting on this door..?", -2, "strawberry", false);
}
}