生成方法中无法识别变量
Variable not recognized in generate method
我在无参数构造函数 AsteroidGame()
中创建了一个 SandBox sb
,但是当我尝试将对象添加到 [=14] 时,在我的生成方法中找不到变量 sb
=].对我哪里出错有任何见解吗?
public static void main(String[] args)
{
new AsteroidGame();
}
public AsteroidGame()
{
//Create SandBox
SandBox sb = new SandBox();
sb.init(this);
}
public void generate()
{
//Instantiate Rocket and add it to Sandbox
Dimension dime = sb.getPanelBounds();
Rocket rock = new Rocket(dime.width/2, dime.height/2);
sb.addBlob(rock);
}
你的 Sanbox
变量是局部变量意味着它在你的构造函数之外是不可见的。如果您想在 generate
函数中使用它,您必须将它转换为 class
的 field
构造函数是初始化同一个class的对象而不是任何class的对象。
Constructor in java is a special type of method that is used to initialize the object.
在 generate
方法本身中创建 sb
。
如果 sb
是字段成员,请使用 sb = new SandBox();
而不是 SandBox sb = new SandBox();
你可以这样做 Declare SandBox as instance/class variable
:
SandBox sb; // Declare SandBox as instance/class variable
public static void main(String[] args)
{
new AsteroidGame();
}
public AsteroidGame()
{
//Create SandBox
sb = new SandBox();
sb.init(this);
}
public void generate()
{
//Instantiate Rocket and add it to Sandbox
Dimension dime = sb.getPanelBounds();
Rocket rock = new Rocket(dime.width/2, dime.height/2);
sb.addBlob(rock);
}
或Create a new local variable in
生成()method:
public static void main(String[] args)
{
new AsteroidGame();
}
public AsteroidGame()
{
//Create SandBox
SandBox sb = new SandBox();
sb.init(this);
}
public void generate()
{
// Create a new local variable here
SandBox sb = new SandBox();
Dimension dime = sb.getPanelBounds();
Rocket rock = new Rocket(dime.width/2, dime.height/2);
sb.addBlob(rock);
}
我在无参数构造函数 AsteroidGame()
中创建了一个 SandBox sb
,但是当我尝试将对象添加到 [=14] 时,在我的生成方法中找不到变量 sb
=].对我哪里出错有任何见解吗?
public static void main(String[] args)
{
new AsteroidGame();
}
public AsteroidGame()
{
//Create SandBox
SandBox sb = new SandBox();
sb.init(this);
}
public void generate()
{
//Instantiate Rocket and add it to Sandbox
Dimension dime = sb.getPanelBounds();
Rocket rock = new Rocket(dime.width/2, dime.height/2);
sb.addBlob(rock);
}
你的 Sanbox
变量是局部变量意味着它在你的构造函数之外是不可见的。如果您想在 generate
函数中使用它,您必须将它转换为 class
field
构造函数是初始化同一个class的对象而不是任何class的对象。
Constructor in java is a special type of method that is used to initialize the object.
在 generate
方法本身中创建 sb
。
如果 sb
是字段成员,请使用 sb = new SandBox();
而不是 SandBox sb = new SandBox();
你可以这样做 Declare SandBox as instance/class variable
:
SandBox sb; // Declare SandBox as instance/class variable
public static void main(String[] args)
{
new AsteroidGame();
}
public AsteroidGame()
{
//Create SandBox
sb = new SandBox();
sb.init(this);
}
public void generate()
{
//Instantiate Rocket and add it to Sandbox
Dimension dime = sb.getPanelBounds();
Rocket rock = new Rocket(dime.width/2, dime.height/2);
sb.addBlob(rock);
}
或Create a new local variable in
生成()method:
public static void main(String[] args)
{
new AsteroidGame();
}
public AsteroidGame()
{
//Create SandBox
SandBox sb = new SandBox();
sb.init(this);
}
public void generate()
{
// Create a new local variable here
SandBox sb = new SandBox();
Dimension dime = sb.getPanelBounds();
Rocket rock = new Rocket(dime.width/2, dime.height/2);
sb.addBlob(rock);
}