Java:无法从同一包中的其他类访问静态变量 [英] Java: cannot access static variable from a different class in the same package
问题描述
这很奇怪,因为我有一个可以访问Frame.dimension.getWidth();的Character类.及其伙伴getHeight(),但是当我想在Map类中使用它时,eclipse会对其加下划线,并且无法给我反馈.无论如何运行该程序都会导致Java错误,表明它们找不到Map对象的X值.
This is quite odd, because I have a Character class that can access Frame.dimension.getWidth(); and its partner getHeight(), however when I wanted to use this in the Map class eclipse underlines it and cannot give me feedback. Running the program anyways ends up in java errors stating they can't find the X value of a Map object.
这是Frame类:
package Core;
import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.JFrame;
public class Frame
{
static int width = 800, height = 600;
static Dimension dimension;
public static void main(String[]args){
JFrame frame= new JFrame("RETRO");
frame.add(new Screen());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width,height);
frame.setVisible(true);
frame.setResizable(false);
dimension = frame.getContentPane().getSize();
}
}
还有Map类:
package Core;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
import java.awt.Dimension;
public class Map
{
double x, y;
Image map;
ImageIcon background = new ImageIcon("images/Maps/maze.jpg");
public Map(){
map = background.getImage();
}
public void move(double moveX, double moveY){
x += moveX;
y += moveY;
}
//////////Gets
public Image getMap(){
return map;
}
public double getX(){
if(x<0)
x=0;
else if(x>Frame.dimension.getWidth())
x=Frame.dimension.getWidth();
return x;
}
public double getY(){
if(y<0)
y=0;
else if(y>Frame.dimension.getHeight())
y=Frame.dimension.getHeight();
return y;
}
public int getHeight(){
return map.getHeight(null);
}
public int getWidth(){
return map.getWidth(null);
}
}
我可以提供Character类,但此刻它很长而且很混乱..但是,单词Frame仅用于Frame.dimension.getWidth()/getHeight();
I could provide the Character class but it is very long and messy at the moment.. however the word Frame was only used in the calling of Frame.dimension.getWidth()/getHeight();
推荐答案
您在 x = Frame.dimension.getWidth();
中引用的Frame类是 java.awt.Frame
.请注意,您导入了此类.尝试显式提及: Core.Frame
,或者如果不使用此类,则删除 import java.awt.Frame;
行.
the Frame class you refer to in x=Frame.dimension.getWidth();
is java.awt.Frame
. note that you imported this class. Try mentioning explicitly: Core.Frame
instead or remove the line import java.awt.Frame;
if you don't use this class.
这篇关于Java:无法从同一包中的其他类访问静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!