我目前正在做这样的事情;
import java.util.*;
public class TestHashMap {
public static void main(String[] args) {
HashMap<Integer, String> httpStatus = new HashMap<Integer, String>();
httpStatus.put(404, "Not found");
httpStatus.put(500, "Internal Server Error");
System.out.println(httpStatus.get(404)); // I want this line to compile,
System.out.println(httpStatus.get(500)); // and this line to compile.
System.out.println(httpStatus.get(123)); // But this line to generate a compile-time error.
}
}
我想确保在我的代码中到处都有 httpStatus.get(n),n 在编译时是有效的,而不是在运行时发现。这可以以某种方式强制执行吗?(我使用纯文本编辑器作为我的“开发环境”。)
我对 Java 很陌生(本周),所以请温柔一点!
谢谢。