Java:从错误的类调用方法

时间:2011-10-17 11:10:07

标签: java

我是Java的新手,我正在尝试编写一个弹跳球程序。它应该在屏幕上的预定位置创建一个球,并且球应该从屏幕上弹开。当我将所有方法放在一个类中时,它完美地工作。但是,当我尝试使用两个类完成任务时,我失败了。

这是Ball类,基本上只是创建新球:

import acm.graphics.*;
import acm.program.GraphicsProgram;
import java.awt.Color;
import java.util.Random;

public class Ball extends GraphicsProgram{

    private GOval ball;
    private int diam;

    public Ball(int width, int height){
        Random rand = new Random();
        diam = rand.nextInt(15) + rand.nextInt(15);
        ball = new GOval(width, height, diam, diam);
        Color c = new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255));
        ball.setFillColor(c);
        ball.setFilled(true);
        add(ball);
    }

    public Ball(){
        Random rand = new Random();
        diam = rand.nextInt(15) + rand.nextInt(15);
        ball = new GOval(50, 100, diam, diam);
        Color c = new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255));
        ball.setFillColor(c);
        ball.setFilled(true);
        add(ball);
    }

    public int getDiam(){
        return(diam);
    }
}

这是应该扩展Ball类的类:

//import acm.graphics.*;
//import acm.program.GraphicsProgram;
import java.awt.Color;
import java.util.Random;

public class BouncingBall extends Ball{

    private final static int START_X = 50;
    private final static int START_Y = 100;

    private final static int GRAVITY = 3;
    private final static double BOUNCE_REDUCE = 0.90;

    private final static int DELAY = 50;

    private final static double X_VEL = 5;

    private double xVel = X_VEL;
    private double yVel = 0.0;

    private Ball ball;
    private int diam;

    public static void main( String[] arg ){
        new BouncingBall().run();
    }

    public void run(){
        setup();
        while(ball.getX() < getWidth()){
            moveBall();
            checkForCollision();
            pause(DELAY);
        }
        remove(ball);
    }

    private void setup(){
        ball = new Ball(START_X, START_Y);
    }

    private void moveBall(){
        yVel += GRAVITY;
        ball.move(xVel, yVel);
    }

    private void checkForCollision(){
        diam = ball.getDiam();
        if (ball.getY() > getHeight() - diam){
            yVel = -yVel * BOUNCE_REDUCE;

            double diff = ball.getY() - (getHeight() - diam);
            ball.move(0, -2 * diff);

            Random rand = new Random();
            Color c = new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255));
            ball.setFillColor(c);
            c = new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255));
            setBackground(c);
        }
    }
}

困扰我的是以下内容。我在行

中的BouncingBall类中遇到错误
ball.move(xVel, yVel);

,行

ball.move(0, -2 * diff);

和行

ball.setFillColor(c);

我得到的错误是“类java.awt.Component中的方法移动不能应用于给定的类型;”前两次出现,后者“无法找到符号”。这两个都是因为move和setFillColor是在错误的类而不是GObject类中查找的,只要我得到它就应该将它的方法提供给Ball继承,然后再传给BouncingBall。

我似乎无法找到解释为什么不会调用继承的方法 - 如果有人可以帮助我,我将不胜感激。

谢谢!

3 个答案:

答案 0 :(得分:3)

您继承了从GraphicsProgram到Ball的课程方法,但假设这是您的GraphicsProgram:http://jtf.acm.org/javadoc/student/acm/program/GraphicsProgram.html它失败,因为GraphicsProgram不包含 move setFillColor 方法而且你也没有给这些方法实现。

答案 1 :(得分:0)

您正在尝试调用该方法 ball.move(xVel, yVel); 其中xVelyVel的类型为double。但该方法需要两个int类型的参数。

请查看相关的API snippet。您可以在调用方法之前将变量声明为int或将值转换为int来解决问题。

答案 2 :(得分:0)

Ball.move 需要整数参数。你发送的双打。在BouncingBall.java中,ball的类型为GraphicsProgram,而非GOval,其方法为setFillColor(Color)