日期:2014-05-20  浏览次数:20597 次

抽象方法的疑惑
Java code

public class GamePanel extends JPanel 
{
    MainFrame mainFrame;
    
    private Image background = ImageUtil.getImage("images/background.jpg");
    
    public GamePanel(MainFrame mainFrame) {
        this.mainFrame = mainFrame;
//        this.setPreferredSize(new Dimension())
    }
    
    public void paint(Graphics g) {
        g.drawImage(this.background, 0, 0, this.getWidth(), 
                this.getHeight() , null);
        Piece currentPiece = this.mainFrame.getCurrentPiece();
        ImageUtil.paintPiece(g, currentPiece);
        Square[][] squares = this.mainFrame.getSquares();
        if (squares == null) return;
        for (int i = 0; i < squares.length; i++) {
            for (int j = 0; j < squares[i].length; j++) {
                Square s = squares[i][j];
                if (s != null) {
                    g.drawImage(s.getImage(), s.getBeginX(), s.getBeginY(), this);
                }
            }
        }
    }

}

这里是一个实现俄罗斯方块背景类,Graphics g 这是一个抽象对象,程序里g还调用了抽象方法g.drawImage,可是我知道抽象方法是没有实际功能的,我把这句注释掉,背景的显示就会出现白版,这就和“抽象方法是没有实际功能”发生矛盾了。这是什么原因呢?

------解决方案--------------------
传入的是他的子类