在Java 7及以后的版本中,可以使用钻石语法(infer types on normally like so without an issue)来推断类型,而不会出现问题。
然而,对于像这样的匿名内部类,它是无法使用的:
List<String> list = new ArrayList<>();
然而,对于像这样的匿名内部类,它是无法使用的:
List<String> st = new List<>() { //Doesn't compile
//Implementation here
}
为什么会这样呢?从逻辑上讲,在这种情况下,我可以确定类型是 String
。是否有逻辑原因导致无法在匿名内部类中推断类型,或者是由于其他原因而被省略了?