简体   繁体   English

没有括号的泛型静态方法,例如在JavaFX 8中

[英]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-方法setCellFactoryAPI 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.

相关问题 可以对类进行反射意外地调用方法(例如静态构造函数)吗? - Can doing reflection on classes accidentally invoke methods (e.g. static constructors)? JavaFX-调整大小和/或移动节点(例如TextFields) - JavaFX - Resize and/or move nodes(e.g. TextFields) 返回Java中通用对象(例如列表)的更干净的解决方案 - Cleaner solution to returning generic objects (e.g. Lists) in Java 方法的数字后缀:它是一种有效的样式吗? (例如 myMethod2()) - Numeric suffix to methods: is it a valid style? (e.g. myMethod2()) 从 AspectJ 访问私有静态成员(例如记录器) - Accessing private static members (e.g. loggers) from AspectJ 在Java Servlet中使用静态变量(例如在AppEngine中) - Usage of Static variables in Java Servlets (e.g. in AppEngine) 没有 npm 的 video.js 插件(例如 eme) - video.js plugins (e.g., eme) without npm Java中数字前的方括号是什么意思? 例如 []89 或 [1, 2, 3]89 - What do square brackets before a number mean in Java? e.g. []89 or [1, 2, 3]89 某些字符(例如括号)将在Spring @RequestBody中进行编码。 如何解码它们? - Some characters (e.g. brackets) are coming encoded in Spring @RequestBody. How to decode them? Java泛型和数组类型,不是你想的(例如来自泛型类型的数组) - Java generic and array types, no not what you are thinking (e.g. arrays from generic types)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM