[英]Java call method
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
}
When i call the add method.the eclipse tell me there are errors existing.i'm confused.i can't find the problem. 当我调用add方法时,日食告诉我存在错误。我很困惑。我找不到问题。
You must call the methods in a function. 您必须在函数中调用方法。
class Game {
Room1 lobby = new Room1("lobby");
Room1 pub = new Room1("pub");
public Game() {
lobby.add("one", pub);
}
}
Wrap the code in a method . 将代码包装在一个方法中。
class Game { Room1 lobby = new Room1("lobby"); Room1 pub = new Room1("pub"); public void init(){ lobby.add("one", pub); //syntax error } }
Use correct syntax 使用正确的语法
public class testing {
public static void main(String arg[]) {
Room1 lobby = new Room1("lobby");
Room1 pub = new Room1("pub");
lobby.add("one", pub);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.