我目前正在做这样的事情;在java中,我想生成一个编译时错误,而不是运行时错误
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(本周)很新,所以请温柔!
谢谢。
枚举常量必须是标识符,所以你必须使用名称,如“r404”和“r500”(或类似的东西)。 – Pointy 2010-11-20 13:25:57
@点好看!感谢您指出了这一点! – coobird 2010-11-20 13:26:57
+1:但我编辑过,以保持一致。 – 2010-11-20 13:42:49