JavaFX 上带有参数的构造函数?
是否可以在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的答案是“不”——创建一个没有设置属性的类的实例是完全合法的——你不能强制出现编译错误。
我不知道你的用例,但如果合适的话,你可以使用 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