[英]Generic static methods without brackets, e.g. in JavaFX 8
I've defined a GUI within a FXML file and have the following code in the corresponding controller: 我已经在FXML文件中定义了一个GUI,并且在相应的控制器中具有以下代码:
@FXML
private TableColumn<Abnormality, String> abnormalityTableViewStatusTableColumn;
later in the initialize()
-method I have: 稍后在
initialize()
方法中,我有:
abnormalityTableViewStatusTableColumn.setCellFactory(
ComboBoxTableCell.<Abnormality, String> forTableColumn("Option1", "Option2", "Option3"));
( API TableColumn - method setCellFactory and API ComboBoxTableCell - method forTableColumn ) ( API TableColumn-方法setCellFactory和API ComboBoxTableCell-方法forTableColumn )
But the compiler would also accept 但是编译器也会接受
abnormalityTableViewStatusTableColumn.setCellFactory(
ComboBoxTableCell.forTableColumn("Option1", "Option2", "Option3"));
without the generic brackets. 没有通用括号。
Does the compiler automatically resolve the types in the second case? 在第二种情况下,编译器是否会自动解析类型?
Thank you for your help! 谢谢您的帮助!
Yes; 是; Java 8 contains some improvements over previous versions in type-inference.
Java 8在类型推断方面对以前的版本进行了一些改进。 In Java 8 the compiler is able to infer from the context (you are passing the result of
forTableColumn
into a method that is expecting a Callback<TableColumn<Abnormality, String>, TableCell<Abnormality, String>>
) that the type for the generic method is <Abnormality, String>
. 在Java 8中,编译器能够从上下文中推断出(您将
forTableColumn
的结果forTableColumn
到期望Callback<TableColumn<Abnormality, String>, TableCell<Abnormality, String>>
)泛型的类型方法是<Abnormality, String>
。 Because the compiler can make this inference, explicitly providing the types is not required. 因为编译器可以进行推断,所以不需要显式提供类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.