【问题标题】:refactoring this function in Java用 Java 重构这个函数
【发布时间】:2011-01-25 18:44:06
【问题描述】:

我正在学习 Java,我知道对于新手程序员的一大抱怨是,我们编写的方法非常冗长且复杂,应该分成几个。好吧,这是我写的一个,是一个完美的例子。 :-D。

public void buildBall(){

        /* sets the x and y value for the center of the canvas */
        double i = ((getWidth() / 2));
        double j = ((getHeight() / 2));

        /* randomizes the start speed of the ball */
        vy = 3.0;
        vx = rgen.nextDouble(1.0, 3.0);
        if (rgen.nextBoolean(.05)) vx = -vx;    

        /* creates the ball */
        GOval ball = new GOval(i,j,(2 *BALL_RADIUS),(2 * BALL_RADIUS));     
        ball.setFilled(true);
        ball.setFillColor(Color.RED);
        add(ball);


        /* animates the ball */ 
        while(true){
            i = (i + (vx* 2));
            j = (j + (vy* 2));  
            if (i > APPLICATION_WIDTH-(2 * BALL_RADIUS)){
                vx = -vx;       
            }

            if (j > APPLICATION_HEIGHT-(2 * BALL_RADIUS)){
                vy = -vy;
            }

            if (i < 0){
                vx = -vx;
            }

            if (j < 0){
                vy = -vy;
            }

            ball.move(vx + vx, vy + vy);
            pause(10);

            /* checks the edges of the ball to see if it hits an object */
            colider = getElementAt(i, j);

            if (colider == null){
                colider = getElementAt(i + (2*BALL_RADIUS), j); 
                }
            if (colider == null){
                colider = getElementAt(i + (2*BALL_RADIUS), j + (2*BALL_RADIUS));   
                }
            if (colider == null){
                colider = getElementAt(i, j + (2*BALL_RADIUS)); 
                }

            /* If the ball hits an object it reverses direction */
            if (colider != null){
                vy = -vy;


             /* removes bricks when hit but not the paddle */
                if (j < (getHeight() -(PADDLE_Y_OFFSET + PADDLE_HEIGHT))){
                remove(colider);
                }

            }

        }

从方法的标题可以看出,我一开始的初衷是“造球”。

我遇到了一些问题:

问题是我需要移动球,所以我创建了那个 while 循环。除了保持“真实”之外,我没有看到任何其他方法可以做到这一点,因此这意味着我在此循环下创建的任何其他代码都不会发生。我没有使 while 循环成为不同的函数,因为我使用了这些变量 i and j

所以我看不出如何在这个循环之外进行重构。

所以我的主要问题是:

我如何将i and j 的值传递给一个新方法:“animateBall”以及我将如何使用 ball.move(vx + vx, vy + vy); 是否在 buildBall 方法中声明了 ball ?

我知道这可能是更好地理解变量范围和传递参数的简单事情,但我还没有完全做到......

【问题讨论】:

  • 嗯......这个词拼写为“collider”而不是“collider”。而且您的代码存在很多样式问题。
  • 您能否更具体地说明您实际上要对“球”对象做什么?比如说,你有一个球对象,你希望它怎么处理,比如球对象应该有哪些方法?这样更容易提供解决方案。
  • @Zaki-saugata 说得对 1) 构建球 2) 让它四处移动 3) 检查它是否击中任何东西。

标签: java methods scope


【解决方案1】:

这可以重构为三种方法 a> build ball :创建球对象并设置初始位置:buildBall() b> 为整个 while 循环设置动画,除了对撞机部分 animate(Ball ball, vx, vy) c> 获取对撞机 getCollider()

由于球是一个对象,并且您已经将 i,j 设置为其字段,因此它们将被传递。 Java 按值传递所有参数。对象存在于堆上;对象引用作为方法参数按值传递。

编辑,添加伪代码

    class Animator{
    void animateBall(){
    Ball ball = buildBall(); //Ball will have i,j,radius etc set by this method
    int vx = randomNumber();
    int vy = randomNumber();
    moveIt(vx,vy, ball);
    }
    void moveIt(int vx, int vy, Ball ball){
        while(true){
             //move the ball, change i,j fields of ball
             //check for collission etc
             Collider collider = getCollider(ball);
             //change direction based on collider etc.
        }
    }
    Collider getCollider(Ball ball){
    //collision code here
    }
}

【讨论】:

  • 对,我明白了。我的问题是 1)我如何将参数从构建球传递给动画球,以及 2)如果前一个函数是一个永远不会出错的 while 循环,它如何到达对撞机?
  • 这很有帮助。感谢您抽出宝贵时间!
  • "对象是通过引用而不是值传递的。" - 这是不正确的。 Java 中的所有内容都是按值传递的。对于对象,它是按值传递的引用。对象本身存在于堆中,并且在传递给方法时不会被复制。
  • @duffymo 我的用词选择不正确 :(,但我认为/希望它能传达这个想法。
  • 如果您同意该回复不正确,我会为后代编辑该回复。
【解决方案2】:

这有点多余

       colider = getElementAt(i, j);

           /* If the ball hits an object it reverses direction */

        if (colider != null && j < (getHeight() -(PADDLE_Y_OFFSET + PADDLE_HEIGHT)))
        {
            vy = -vy;

         /* removes bricks when hit but not the paddle */
            remove(colider);
         }


            else
            {
                colider = getElementAt(i + (2*BALL_RADIUS), j); 
                colider = getElementAt(i + (2*BALL_RADIUS), j + (2*BALL_RADIUS));  
                colider = getElementAt(i, j + (2*BALL_RADIUS));      
            }

【讨论】:

    猜你喜欢
    • 2018-03-27
    • 2011-03-29
    • 2015-07-02
    • 2018-04-01
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    相关资源
    最近更新 更多