简体   繁体   English

JavaFx 和 @FXML 中的访问修饰符

[英]Access modifiers in JavaFx and @FXML

I am new to JavaFx and in a few tutorials I've seen, there were some differences.我是 JavaFx 的新手,在我看过的一些教程中,存在一些差异。 In one of the tutorials, he always used private as the access modifier in the controller class and wrote it this way:在其中一个教程中,他总是在 controller class 中使用private作为访问修饰符,并这样写:

@FXML private Button button;

But the other tutorial always used public and didn't include @FXML in the controller class.但是另一个教程总是使用 public 并且没有在 controller class 中包含@FXML。 Both of these seem to work fine but is there an actual difference between them that I should know?这两个似乎都可以正常工作,但是我应该知道它们之间的实际区别吗?

From Introduction to FXML :FXML 简介

@FXML

Note that, in the previous examples, the controller member fields and event handler methods were declared as public so they can be set or invoked by the loader.请注意,在前面的示例中,controller 成员字段和事件处理程序方法被声明为公共,因此它们可以由加载程序设置或调用。 In practice, this is not often an issue, since a controller is generally only visible to the FXML loader that creates it.实际上,这通常不是问题,因为 controller 通常仅对创建它的 FXML 加载程序可见。 However, for developers who prefer more restricted visibility for controller fields or handler methods, the javafx.fxml.FXML annotation can be used.但是,对于更喜欢 controller 字段或处理程序方法的可见性受限的开发人员,可以使用javafx.fxml.FXML注释。 This annotation marks a protected or private class member as accessible to FXML.此注释将受保护或私有 class 成员标记为可访问 FXML。 If the class being annotated is in a named module, the module containing that class must open the containing package to at least the javafx.fxml module. If the class being annotated is in a named module, the module containing that class must open the containing package to at least the javafx.fxml module.

In other words, the @FXML annotation is only required if the field or method is non-public (ie is protected, package-private, or private) yet needs to be accessible to FXML.换句话说,只有当字段或方法是非公共的(即受保护的、包私有的或私有的)但需要 FXML 访问时,才需要@FXML注释。 Within the context of FXML, there is no difference between a public field/method with no (or even with an) @FXML annotation and a non-public field/method with said annotation.在 FXML 的上下文中,没有(甚至没有) @FXML注释的公共字段/方法与具有所述注释的非公共字段/方法之间没有区别。 The only difference in general is the visibility of the field/method to other code.通常唯一的区别是字段/方法对其他代码的可见性。

That said, it's typically considered good practice to only make something as visible as it needs to be.也就是说,通常认为只使某些东西在需要时可见是一种好的做法。 An FXML-injected field normally has no reason to be public and neither does an event-handler method—they're implementation details.注入 FXML 的字段通常没有公开的理由,事件处理程序方法也没有——它们是实现细节。

Note the @FXML annotation doesn't do anything special on a language level.请注意, @FXML注释在语言级别上没有做任何特殊的事情。 The presence of the annotation simply tells the FXMLLoader it's okay to try and reflectively access the field or method even though it's not public.注释的存在只是告诉FXMLLoader可以尝试反射性地访问字段或方法,即使它不是公共的。 It's also a good hint to the developer that the field or method is handled by FXML (for instance, FXML-injected fields should virtually never be manually initialized or reassigned).这也是对开发人员的一个很好的提示,即字段或方法由 FXML 处理(例如,FXML 注入的字段实际上不应该被手动初始化或重新分配)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM