猿问

Java调用方法

import java.util.HashMap;

   class Room1 {

       private String description;

       private HashMap<String, Room1> dir = new HashMap<String, Room1>();

       Room1(String de) {

           description = de;

       }

       public String toString() {

           return description;

       }

       public void add(String s, Room1 r) {

           dir.put(s, r);

       }

   }

   class Game {

       Room1 lobby = new Room1("lobby");

       Room1 pub = new Room1("pub");

       lobby.add("one", pub); //syntax error

   }

当我调用 add 方法时,eclipse 告诉我存在错误。我很困惑。我找不到问题。


DIEA
浏览 190回答 3
3回答

繁华开满天机

您必须在函数中调用这些方法。class Game {&nbsp; &nbsp; Room1 lobby = new Room1("lobby");&nbsp; &nbsp; Room1 pub = new Room1("pub");&nbsp; &nbsp; public Game() {&nbsp; &nbsp; &nbsp; &nbsp; lobby.add("one", pub);&nbsp; &nbsp; }}

慕神8447489

使用正确的语法public class testing {public static void main(String arg[]) {&nbsp; &nbsp; Room1 lobby = new Room1("lobby");&nbsp; &nbsp; Room1 pub = new Room1("pub");&nbsp; &nbsp; lobby.add("one", pub);&nbsp; }&nbsp;&nbsp;}
随时随地看视频慕课网APP

相关分类

Python
我要回答