在类之间共享变量
我有一个听起来很愚蠢但不知道为什么它不起作用的问题。我有 2 个类,Directory 和 Map,在 Directory 类内部有一个本地字符串变量,它获取目录当前文件夹的名称,并希望将其放在显示图形的 Map 类中的矩形内。将变量放入Map类后,问题是字符串为空。
这是代码:
public class Directory
{
public static File directory; // the directory that we want to use
public static String dirName = directory.getName();
public String test = "test";
public Directory(File directory)
{
files = directory.listFiles();
}
}
public class Test extends JComponent
{
Directory dir = new Directory(null);
public Test()
{
JFrame frame = new JFrame("Test");
frame.setSize(800,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(this);
frame.setVisible(true);
}
public void paintComponent(Graphics g)
{
super.paintComponents(g);
g.setColor(Color.yellow);
g.fillRect(10, 10, 100, 100);
g.drawString(dir.tigka, 10, 20);
}
}
I have a problem that sounds stupid but don't know why it does not work. I have 2 classes, Directory and Map, and inside the Directory class there is a local String variable that takes the name of the current folder of the directory and want to put it inside a rectangle in the Map class that displays the graphics. After putting the variable into the Map class, the problem is that the string is empty.
Here is the code :
public class Directory
{
public static File directory; // the directory that we want to use
public static String dirName = directory.getName();
public String test = "test";
public Directory(File directory)
{
files = directory.listFiles();
}
}
public class Test extends JComponent
{
Directory dir = new Directory(null);
public Test()
{
JFrame frame = new JFrame("Test");
frame.setSize(800,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(this);
frame.setVisible(true);
}
public void paintComponent(Graphics g)
{
super.paintComponents(g);
g.setColor(Color.yellow);
g.fillRect(10, 10, 100, 100);
g.drawString(dir.tigka, 10, 20);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用 (null) 实例化了一个 Directory 对象,然后使用该实例来设置 String 变量。零值通过是完全有道理的。
You instantiated a Directory object with (null), and then used that instance to set the String variable. Makes perfect sense that the null passes though.
这是我发现的一种新解决方案(我是初学者),用于为应用程序中的所有类维护一个中央动态变量位置,无论使用相同的变量多少次或在何处或何时使用它们。听起来它可能适用于解决方案?
https://stackoverflow.com/questions/17861704/solution-类间共享变量?noredirect=1#comment26078064_17861704
This is a new solution I discovered (I'm a beginner) for maintaining one central, dynamic variable location for all classes in an application, regardless of how many times the same variables are used or where or when they are used. It sounds like it might apply to a solution?
https://stackoverflow.com/questions/17861704/solution-for-sharing-variables-between-classes?noredirect=1#comment26078064_17861704
奇怪的问题,很难评估答案,因为目标变化太大..尽管如此,还是忍不住投入了我的几美分(欧元!),因为我认为 NPE 的原因尚未被发现
代码我指的是(如果它在我写作时发生变化)
用空文件实例化看起来像是罪魁祸首......但从未达到:抛出的第一个 NPE 来自 dirName 的静态初始化,它访问静态字段目录是.. 好吧,null
编辑(截至@Eristikos 评论):删除了静态类修饰符以与原始示例保持同步。
weird question and hard to evaluate the answers, as the target is changing so much .. nevertheless, can't resist throwing in my couple of cents (Euro!) as well because I think the reason for the NPE is not yet spotted
The code I'm referring to is (in case it's changing while I'm writing)
instantiating with a null file looks like the culprit ... but is never reached: the first NPE thrown comes from the static initialization of dirName which accesses the static field directory which is .. well, null
Edit (as of @Eristikos comment): removed the static class modifier to stay in synch with the original example.