【问题标题】:Object initializes despite wrong values to constructor?尽管构造函数的值错误,但对象初始化?
【发布时间】:2013-10-29 09:46:08
【问题描述】:
Timeline pongAnimation = TimelineBuilder.create()
    .keyFrames(
      new KeyFrame(
        new Duration(10.0),
        new EventHandler<ActionEvent>() {
          public void handle(javafx.event.ActionEvent t) {
            checkForCollision();
            int horzPixels = movingRight ? 1 : -1;
            int vertPixels = movingDown ? 1 : -1;
            centerX.setValue(centerX.getValue() + horzPixels);
            centerY.setValue(centerY.getValue() + vertPixels);
          }
        }
      )
    )
    .cycleCount(Timeline.INDEFINITE)
    .build();  

这是我正在阅读的书中的 JavaFX 代码。它通过传递DurationEventListener 来创建KeyFrame——不多也不少。

EventHandler 关联的Timeline 类的所有构造函数都需要KeyValues 作为参数。但是,在上面的代码中并非如此。代码编译,甚至给出所需的输出。

为什么?

文档:http://docs.oracle.com/javafx/2/api/javafx/animation/KeyFrame.html

【问题讨论】:

  • 这段代码是否符合您的预期取决于TimelineBuilder 类中的内容。您需要查看该类以了解这里发生了什么。

标签: java generics constructor arguments javafx


【解决方案1】:

你使用的构造函数是

public KeyFrame(Duration time,
        EventHandler<ActionEvent> onFinished,
        KeyValue... values)

参数KeyValue... 是一个可变参数。如果你不向方法传递任何参数,它将是一个空数组。

【讨论】:

  • 所以,我什至不需要指定我正在传递null 或其他什么?
  • @LittleChild 如果你这样称呼它,values 将是 new KeyValue[0]。如果您在构造函数调用中将null 作为第三个参数传递,构造函数将收到new KeyValue[] {null}
猜你喜欢
  • 1970-01-01
  • 2012-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-06
  • 1970-01-01
  • 2012-08-14
  • 2021-09-10
相关资源
最近更新 更多