gpt4 book ai didi

java - Java Netbeans 变量继承问题

转载 作者:行者123 更新时间:2023-12-01 08:06:49 25 4
gpt4 key购买 nike

所以我四处寻找这个错误的答案,但没有找到确切的答案。我是 Java 和 Netbeans 新手,到目前为止我所做的一切都是在 BlueJ 上完成的。当我将一个类扩展到另一个类时,应该继承变量和方法,但我不断收到变量未找到错误。这是父类(super class):

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 中运行得很好。

最佳答案

您的包名称不同 runner2dRunner2D 您的类应该位于同一包下,或者您应该将一个类导入到另一个包中。

关于java - Java Netbeans 变量继承问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20876042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com