在Java中重构此函数
我正在学习Java,我知道对新手程序员最大的抱怨之一是我们制作了非常长且复杂的方法,这些方法应该分成几个部分。这是我写的一个很好的例子-D在Java中重构此函数,java,methods,scope,Java,Methods,Scope,我正在学习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));
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);
}
}
}
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));
}
public void buildBall(){
/*设置画布中心的x和y值*/
双i=((getWidth()/2));
双j=((getHeight()/2));
/*随机化球的起始速度*/
vy=3.0;
vx=rgen.nextDouble(1.0,3.0);
如果(rgen.nextBoolean(.05))vx=-vx;
/*创建球*/
GOval ball=新的GOval(i,j,(2*ball_半径),(2*ball_半径));
ball.setFilled(真);
球形。setFillColor(颜色。红色);
添加(球);
/*为球设置动画*/
while(true){
i=(i+(vx*2));
j=(j+(vy*2));
如果(i>应用程序宽度-(2*球半径)){
vx=-vx;
}
如果(j>应用高度-(2*球半径)){
vy=-vy;
}
if(i<0){
vx=-vx;
}
if(j<0){
vy=-vy;
}
球移动(vx+vx,vy+vy);
暂停(10);
/*检查球的边缘,以查看它是否碰到对象*/
colider=getElementAt(i,j);
if(colider==null){
colider=getElementAt(i+(2*BALL_半径),j);
}
if(colider==null){
colider=getElementAt(i+(2*球半径),j+(2*球半径));
}
if(colider==null){
colider=getElementAt(i,j+(2*BALL_半径));
}
/*如果球碰到物体,它会反转方向*/
if(colider!=null){
vy=-vy;
/*击打时移除砖块,但不移除桨叶*/
如果(j<(getHeight()-(桨叶Y_偏移+桨叶高度))){
移除(colider);
}
}
}
你可以从这个方法的标题中看到,我开始的时候是出于“造球”的良好意图
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));
}
我遇到了几个问题:
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));
}
问题是,然后我需要移动球,所以我创建了while循环。除了保持它为“true”之外,我没有看到任何其他方法可以做到这一点,这意味着我在这个循环下面创建的任何其他代码都不会发生。我没有使while循环成为一个不同的函数,因为我使用了这些变量I和j
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));
}
因此,我不知道如何在这个循环之外进行重构
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));
}
所以我的主要问题是:
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));
}
如何将I和j的值传递给一个新方法:“animateBall”,以及如何使用
ball.move(vx+vx,vy+vy);
如果在buildBall方法中声明了ball,那么在该新方法中移动(vx+vx,vy+vy);
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));
}
我知道这可能是一件简单的事情,可以更好地理解变量范围和传递参数,但我还不太清楚…这是一件不太冗余的事情
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));
}
colider=getElementAt(i,j);
/*如果球碰到物体,它会反转方向*/
如果(colider!=null&&j<(getHeight()-(桨距Y偏移+桨距高度)))
{
vy=-vy;
/*击打时移除砖块,但不移除桨叶*/
移除(colider);
}
其他的
{
colider=getElementAt(i+(2*BALL_半径),j);
colider=getElementAt(i+(2*球半径),j+(2*球半径));
colider=getElementAt(i,j+(2*BALL_半径));
}
这可以重构为三种方法
a> 构建球:创建球对象并设置初始位置:buildBall()
b> 为整个while循环设置动画,但碰撞器部分除外(Ball-Ball、vx、vy)
c> 获取碰撞器getCollider()
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));
}
由于ball是一个对象,并且您已经将i,j设置为将要传递的字段。Java通过值传递所有参数。对象位于堆上;对象引用通过值作为方法参数传递
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));
}
编辑,添加伪代码
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));
}
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
}
}
这个词的拼写是“对撞机”而不是“科利德”。你的代码有很多风格问题。你能不能更具体一点,你到底想用“ball”对象做什么?比如说,你有一个ball对象,你希望它是怎样的bahave,比如ball对象应该有什么方法?这样会更容易提供解决方案。@Zaki saugata说得差不多1) 构建球2)让它四处移动3)检查它是否击中任何东西。对-我明白了。我的问题是1)如何将参数从构建球传递到设置球的动画,以及2)如果前面的函数是一个永不出错的while循环,它如何到达碰撞器?这非常有帮助。感谢您花时间!“对象是通过引用传递的,而不是通过值传递的。”-这是不正确的。Java中的所有内容都是通过值传递的。对于对象,它是一个通过值传递的引用。对象本身存在于堆中,并且在传递到方法时不会被复制。@duffymo我选择的词不正确:)但是我想/希望它能让人明白这个想法。如果你同意这个回答是错误的,我会为后代编辑这个回答。