为什么我无法访问在构造函数中创建的类实例(Java)

huangapple go评论75阅读模式
英文:

Why can I not access an instance of class that is created in a constructor ( Java )

问题

以下是您要翻译的内容:

为了引言这个问题,我对Java非常陌生。

我有三个类,分别是Game、Player和SystemIO。
我的main()方法在Game类中。以下是它的代码:

public static void main(String[] args){
SystemIO systemIO = new SystemIO();
}

一旦调用SystemIO,它的构造函数就会用以下代码创建Player的一个实例:

 Player player = new Player("Bob");

其中Player的构造函数以一个字符串作为参数。

在SystemIO类的后面,我有一个方法可以访问“player”实例的信息:

player.getName();

当我尝试这样做时,控制台报告如下错误:

SystemIO.java:339: error: cannot find symbol

我已经检查过,我没有试图使用大写的“Player”类名进行引用。
正如我所说,我对Java非常陌生,只是在努力理解它,我认为这是一个作用域问题...但我不确定。

编辑以添加可复现的代码:

Game.java

package com.myapps;
import com.myapps.system.SystemIO;

public class Game{
    public static void main(String[] args){
        SystemIO systemIO = new SystemIO();
    }
}

Player.java

package com.myapps.player;

public class Player{
    String name;
    public Player(String playerName){
        name = playerName;
    }
}

public String getName(){
    return name;
}

SystemIO.java

package com.myapps.system;
import com.myapps.player.Player;

public class SystemIO{
    public SystemIO(){
        Player player = new Player("Bob");
        readPlayerName();
    }

    public void readPlayerName(){
        System.out.println(player.getName());
    }
}
英文:

To preface the question, I'm very new to Java.

I have classes called, Game, Player, and SystemIO.
My main() is in the Game class. Below is it's code <br />

public static void main(String[] args){
SystemIO systemIO = new SystemIO();
}

Once SystemIO is called, it's constructor creates an instance of Player with the line<br />

 Player player = new Player(&quot;Bob&quot;);

<br />
where the Player constructor takes 1 argument as a String.

Further down in the SystemIO class I have a method that accesses information from "player" the instance.<br />

player.getName();

When I try to do this, the console reports <br />
SystemIO.java:339: error: cannot find symbol

I have checked that I am not trying to reference the class name with a capital "Player."
Like I said, I'm extremely new to Java and just trying to wrap my head around it and I believe it's a scope issue...but I'm not sure.

Edit to add reproducible code:

Game.java

package com.myapps;
import com.myapps.system.SystemIO;

public class Game{
    public static void main(String[] args){
        SystemIO systemIO = new SystemIO();
    }
}

Player.java

package com.myapps.player;

public class Player{
    String name;
    public Player(String playerName){
        name = playerName;
    }
}

public String getName(){
    return name;
}

SystemIO.java

package com.myapps.system;
import com.myapps.player.Player;

public class SystemIO{
    public SystemIO(){
        Player player = new Player(&quot;Bob&quot;);
        readPlayerName();
    }

    public void readPlayerName(){
        System.out.println(player.getName());
    }
}

答案1

得分: 1

Make player a class variable.

将player设为类变量。

Put someone in your class:

将某人放入你的类:

Player player;

and change the code of your constructor to:

并且将你构造函数的代码更改为:

player = new Player("Bob");

This is called a scope error. A variable that you want to be accessable to ALL the methods of the class, should be declared IN the class and not in ONE specific method (in your case, you did it in the constructor)

这被称为作用域错误。你想要在类的所有方法中都能访问的变量,应该在类中声明,而不是在一个特定的方法中(在你的情况下,你是在构造函数中这样做的)。

英文:

Make player a class variable.

Put someone in your class:

Player player;

and change the code of your constructor to:

player = new Player(&quot;Bob&quot;);

This is called a scope error. A variable that you want to be accessable to ALL the methods of the class, should be declared IN the class and not in ONE specific method (in your case, you did it in the constructor)

huangapple
  • 本文由 发表于 2020年4月4日 07:10:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/61021750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定