Ich mache gerade etwas wie das;In Java möchte ich einen Kompilierungsfehler anstelle eines Laufzeitfehlers erzeugen
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.
}
}
ich, dass überall in meinem Code sicherstellen will, dass es ein httpStatus.get (n) ist, dass n zur Compile-Zeit gültig statt findet später zur Laufzeit aus. Kann das irgendwie durchgesetzt werden? (Ich benutze einen einfachen Texteditor als meine "Entwicklungsumgebung".)
Ich bin sehr neu in Java (diese Woche) also bitte sei sanft!
Danke.
Enum-Konstanten müssen Bezeichner sein, also müssten Sie Namen wie "r404" und "r500" (oder so ähnlich) verwenden. – Pointy
@Pointy Netter Fang! Danke, dass du das unterstrichen hast! – coobird
+1: Aber ich bearbeitet, um es für Sie konsistent zu machen. –