Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:某些代码语法有问题_Java - Fatal编程技术网

Java:某些代码语法有问题

Java:某些代码语法有问题,java,Java,为了完整起见,我在下面提供了完整的代码 请帮我用英语翻译以下内容: protected void moveBall() { for (Node node:this.getChildren()) { Ball ball = (Ball)node; //其他一些代码 以下是我(不正确)的解释: 获取对节点的引用,并将引用设置为等于该类中的所有节点 获取对Ball的引用,并将其设置为node,该节点将被转换为球 除了不知道如何解释上述代码外,我也不太理解它的功

为了完整起见,我在下面提供了完整的代码

请帮我用英语翻译以下内容:

protected void moveBall() {
        for (Node node:this.getChildren()) {
            Ball ball = (Ball)node;
//其他一些代码

以下是我(不正确)的解释:

获取对节点的引用,并将引用设置为等于该类中的所有节点

获取对Ball的引用,并将其设置为node,该节点将被转换为球

除了不知道如何解释上述代码外,我也不太理解它的功能

第二个问题是关于circle类:

class Ball extends Circle {
    private double dx = 1;
    private double dy = 1;

    public Ball (double x, double y, double radius, Color color) {
        super(x,y,radius);
        setFill(color);
        }
    }
调用super方法是否等同于以下内容:

this.x = x;
this.y = y;
this.radius = radius;
为什么

公共类多路广播扩展了应用程序{

public void start(Stage primaryStage) {
    MultipleBallsPane ballsPane = new MultipleBallsPane();
    Button btnAddBall = new Button("+");
    Button btnRemoveBall = new Button("-");

    HBox hBox = new HBox();
    hBox.getChildren().addAll(btnAddBall, btnRemoveBall);
    hBox.setAlignment(Pos.CENTER);

    //add or remove ball

    btnAddBall.setOnMousePressed(e -> ballsPane.add());
    btnRemoveBall.setOnMousePressed(e -> ballsPane.subtract());

    //resume and pause animation

    ballsPane.setOnMousePressed(e -> ballsPane.pause());
    ballsPane.setOnMouseReleased(e -> ballsPane.play());

    //scroll bar to control animation speed

    ScrollBar sbSpeed = new ScrollBar();
    sbSpeed.setMax(20);
    sbSpeed.setMin(10);
    ballsPane.rateProperty().bind(sbSpeed.valueProperty());

    HBox hBox2 = new HBox();
    hBox2.getChildren().addAll(sbSpeed);
    hBox2.setAlignment(Pos.CENTER);


    BorderPane pane = new BorderPane();
    pane.setTop(hBox2);
    pane.setCenter(ballsPane);
    pane.setBottom(hBox);

    Scene scene = new Scene(pane,250, 150);
    primaryStage.setScene(scene);
    primaryStage.show();

}

public static void main(String[] args) {
    Application.launch(args);
}

// inner class MultipleBallsPane

private class MultipleBallsPane extends Pane {
    private Timeline animation;

    public MultipleBallsPane() {
        //create an animation for moving the ball
        animation = new Timeline(
                new KeyFrame(Duration.millis(50), e->moveBall()));
        animation.setCycleCount(Timeline.INDEFINITE);
        animation.play();

    }

    public void add() {
        Color color = new Color(Math.random(), 
                Math.random(), Math.random(), 0.5);
        getChildren().add(new Ball(30,30,20, color));

    }

    public void subtract() {
        if (getChildren().size() > 0 ) {
        getChildren().remove(getChildren().size() - 1);
        }
    }
    public void play() {
        animation.play();   
    }

    public void pause() {
        animation.pause();
    }

    public void increaseSpeed() {
        animation.setRate(animation.getRate() + 0.5);
    }

    public void decreaseSpeed() {
        if (animation.getRate() > 0) {
            animation.setRate(animation.getRate() - 0.5);
        }
    }


    public DoubleProperty rateProperty() {
        return animation.rateProperty();        

    }

    protected void moveBall() {
        for (Node node:this.getChildren()) {
            Ball ball = (Ball)node;


        //check boundaries
        if (ball.getCenterX() < ball.getRadius() ||
                ball.getCenterX() > getWidth() - ball.getRadius()) {
            ball.dx*= -1;   
        }

        if (ball.getCenterY() < ball.getRadius() ||
                ball.getCenterY() >getHeight() - ball.getRadius()) {
            ball.dy*= -1;
        }

        ball.setCenterX(ball.dx + ball.getCenterX());
        ball.setCenterY(ball.dy + ball.getCenterY());

    }
} // end of method

class Ball extends Circle {
    private double dx = 1;
    private double dy = 1;


    public Ball (double x, double y, double radius, Color color) {
        super(x,y,radius);
        setFill(color);
        }
    }
} // end of MultipleBallsPane class
公共作废开始(阶段primaryStage){
MultipleBallsPane ballsPane=新的MultipleBallsPane();
按钮btnAddBall=新按钮(“+”);
按钮btnRemoveBall=新按钮(“-”);
HBox HBox=新的HBox();
hBox.getChildren().addAll(btnAddBall,btnRemoveBall);
hBox.设置校准(位置中心);
//添加或删除球
btnAddBall.setOnMousePressed(e->ballsPane.add());
btnRemoveBall.setOnMousePressed(e->ballsPane.subtract());
//恢复和暂停动画
ballsPane.setOnMousePressed(e->ballsPane.pause());
ballsPane.setOnMouseReleased(e->ballsPane.play());
//控制动画速度的滚动条
滚动条sbSpeed=新滚动条();
sbSpeed.setMax(20);
sbSpeed.setMin(10);
ballsPane.rateProperty().bind(sbSpeed.valueProperty());
HBox hBox2=新的HBox();
hBox2.getChildren().addAll(sbSpeed);
hBox2.设置校准(位置中心);
BorderPane=新的BorderPane();
面板。机顶盒(hBox2);
窗格。设置中心(圆珠笔);
窗格.立板条箱(hBox);
场景=新场景(窗格,250,150);
初级阶段。场景(场景);
primaryStage.show();
}
公共静态void main(字符串[]args){
应用程序启动(args);
}
//内类多重边界
专用类MultipleBallsPane扩展窗格{
私人时间轴动画;
公共多路广播(){
//创建用于移动球的动画
动画=新时间线(
新的关键帧(Duration.millis(50),e->moveBall());
animation.setCycleCount(Timeline.unfinite);
动画。播放();
}
公共无效添加(){
颜色=新颜色(Math.random(),
Math.random(),Math.random(),0.5);
getChildren().add(新球(30,30,20,颜色));
}
公共空减(){
如果(getChildren().size()>0){
getChildren().remove(getChildren().size()-1);
}
}
公共游戏{
动画。播放();
}
公共空间暂停(){
暂停();
}
public void increaseSpeed(){
animation.setRate(animation.getRate()+0.5);
}
公共空间减少速度(){
如果(animation.getRate()>0){
animation.setRate(animation.getRate()-0.5);
}
}
公营物业差饷物业(){
返回animation.rateProperty();
}
受保护的void moveBall(){
对于(节点:this.getChildren()){
球=(球)节点;
//检查边界
如果(ball.getCenterX()getWidth()-ball.getRadius()){
ball.dx*=-1;
}
if(ball.getCenterY()getHeight()-ball.getRadius()){
ball.dy*=-1;
}
ball.setCenterX(ball.dx+ball.getCenterX());
ball.setCenterY(ball.dy+ball.getCenterY());
}
}//方法结束
类球延伸圆{
专用双dx=1;
私人双dy=1;
公共球(双x、双y、双半径、彩色){
super(x,y,半径);
设置填充(颜色);
}
}
}//MultipleBallsPane类的结束
}关于这一点:

protected void moveBall() {
    for (Node node: this.getChildren()) {
        Ball ball = (Ball)node;
本例中的
for
循环更像是一个
foreach
,它通过子节点中的每个节点。对于每个节点,通过将
节点
投射到
来创建
对象。用简单的英语:

protected void moveBall() {
        for (Node node:this.getChildren()) {
            Ball ball = (Ball)node;
对于该对象具有的子对象集中的每个节点,将每个节点投射为一个球

Casting本质上是说一种数据类型(在本例中为
节点
)将被视为另一种数据类型(在本例中为
)。有关铸造是什么的更多信息,请参考:

2)
super()
调用父构造函数,在本例中是
Circle
。由于没有附加构造函数,这只是猜测,但相信
构造函数会执行您指定的函数是合乎逻辑的。下面是super()的一个很好的参考:

希望这有帮助

这将解码为:

使用
this.getChildren()
获取
this
的所有子节点,并将第一个子节点分配给
节点

然后运行下一行,该行创建一个局部变量
ball
,该变量等于
节点
,然后将其浇铸为一个球

执行其他代码

重复上述步骤,直到不再剩下孩子

至于圆,当您调用
super
时,它可能没有那么简单,超级构造函数可能会设置变量,或者它可能会执行一些其他逻辑。所以在我看来,调用构造函数会更安全

  • 库利普

假定以下语法正确:

protected void moveBall() {
    for (Node node:this.getChildren()) {
        Ball ball = (Ball)node;
    }
}
这可以理解为:

对于调用
getChildren()
(此处引用为
node
且类型为
node
)的iterable/数组中的每个元素,将
node
的手动转换分配给类型为
Ball
的变量
Ball

第二:

没有看到