所以我四处寻找这个错误的答案,但没有提出任何确切的答案。 我是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中完全没问题。
答案 0 :(得分:6)
您的软件包名称不同runner2d
和Runner2D
您的类应该在同一个软件包下,或者您应该将其导入到另一个软件包中。