重写JPanel的getX()方法后有什么后果?比如说这样重写:public int getX (){
    return x;//此处x为显示在JPanel上的某物体的坐标
}这样调用:snake.setX(snake.getX());//snake是Snake的实例,继承了JPanel,我的重写便是在Snake里进行的;x是snake在JPanel上                            显示的坐标我运行后发现snake显示到屏幕一半时会变成一条缝把getX()重命名为getSnakeX()后恢复正常求大神解释!!!!

解决方案 »

  1.   

    文档中是这么说的:public int getX()
        Returns the current x coordinate of the component's origin. This method is preferable to writing component.getBounds().x, or component.getLocation().x because it doesn't cause any heap allocations.
        Overrides:
            getX in class Component
        Returns:
            the current x coordinate of the component's origin这个方法不是不能覆盖,问题在于你的代码是如何用它的,它应该用于获取控件左上角的X坐标。