JavaFX 上带有参数的构造函数?

发布于 2024-09-19 15:25:37 字数 427 浏览 3 评论 0原文

是否可以在 JavaFX 类上创建带有参数的构造函数?

这可以在 Java 中通过以下方式实现:

   MyObj(String foo, String bar) {
       // ... 
   }

这在 JavaFX 中可能吗?

IE

   // creating an Object Literal without the specified arguments must result in compile error
   var myObj = MyObj {} ;

   // While this should not result in compile error
   var myObj2 = MyObj {foo: "foo", bar: "bar"};

Is it possible to create a constructor with arguments on a JavaFX class?

This can be achieved in Java by:

   MyObj(String foo, String bar) {
       // ... 
   }

Is this possible in JavaFX?

i.e.

   // creating an Object Literal without the specified arguments must result in compile error
   var myObj = MyObj {} ;

   // While this should not result in compile error
   var myObj2 = MyObj {foo: "foo", bar: "bar"};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃气十足 2024-09-26 15:25:37

简短的答案是“不”——创建一个没有设置属性的类的实例是完全合法的——你不能强制出现编译错误。

我不知道你的用例,但如果合适的话,你可以使用 postinit 块在对象创建后设置各种默认值,或者使用常规 Java 对象 - 你当然可以从 JavaFX 实例化 Java 对象,并且它们受编译检查

The short answer is "no" - it is perfectly legal to create an instance of a class with no properties set on it - you can't force a compilation error.

I don't know your use case, but you could use a postinit block to set various defaults after object creation if that is suitable, or use a regular Java object - you can of course instantiate Java objects from JavaFX, and they are subject to compilation checks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文