【发布时间】:2014-01-02 03:48:05
【问题描述】:
所以我四处寻找这个错误的答案,但没有找到确切的答案。 我是 Java 和 Netbeans 的新手,到目前为止我所做的一切都在 BlueJ 上。当我将一个类扩展到另一个类时,应该继承变量和方法,但我不断收到未找到变量的错误。 这是超类:
package Runner2D;
import java.awt.*;
public class Block {
protected boolean power;
public int width;
public int height;
public int xPos;
public int yPos;
public boolean hit;
public Block( int x, int y ){
xPos = x;
yPos = y;
width = 30;
height = 30;
power = false;
} // end Block
public Block( ){
xPos = ( int ) ( Math.random() * 501 );
yPos = ( int ) ( Math.random() * 501 );
width = 40;
height = 40;
} // end Block
public void drawSquare( Graphics2D g2 ){
g2.fillRect( xPos, yPos, width, height );
} // end
} // end Block
这是子类:
package runner2d;
import java.awt.*;
public class Invincibility extends Block{
public Invincibility( int x, int y ){
super( x, y );
power = true;
hit = false;
} // end Invinsibility
public void setHit( boolean b ){
hit = b;
} // end setHit
public void drawSquare( Graphics2D g2 ){
if ( !hit ) g2.fillRect( xPos, yPos, width, height );
else xPos = - 40;`enter code here`
} // end drawSquare
} // end class
确切的错误是找不到符号。这在 BlueJ 中运行良好。
【问题讨论】:
-
对于哪个变量/方法?
标签: java inheritance netbeans subclass super