开发者

Constructor with arguments on JavaFX?

开发者 https://www.devze.com 2023-01-16 01:21 出处:网络
Is it possible to create a constructor with arguments on a JavaFX class? This can be achieved in Java by:

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 resu开发者_运维百科lt in compile error
   var myObj2 = MyObj {foo: "foo", bar: "bar"};


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

0

精彩评论

暂无评论...
验证码 换一张
取 消