为什么我的Java程序无法识别特定类中的静态变量?

时间:2017-08-17 22:56:15

标签: java variables compiler-errors static-variables javax

我正在使用Javax为库存系统(我计划稍后开始游戏)做一些测试代码 组件。我正在创建一个名为'Cursor'的类,它扩展了'ItemStack',以便你可以“拾取” 您在库存屏幕中单击的项目。我遇到的问题是我使用静态变量 'cur'用于保存此类的实例,但每次尝试编译时,我都会得到以下结果 错误:

.\john\window\component\ItemSlotPanel.java:13: error: cannot find symbol
    itemStack.swapWith(Cursor.cur);
                             ^
  symbol:   variable cur
  location: class Cursor
.\john\window\component\ItemSlotPanel.java:25: error: package itemstack.item doe
s not exist
      g.drawImage(itemstack.item.icon.getScaledInstance(this.getWidth(),this.get
Height(),Image.SCALE_SMOOTH),0,0,null);
                                ^
.\john\window\component\InfiniteItemSlotPanel.java:15: error: cannot find symbol

    if (Cursor.cur.isEmpty()) {
              ^
  symbol:   variable cur
  location: class Cursor
.\john\window\component\InfiniteItemSlotPanel.java:16: error: cannot find symbol

      this.itemStack.copyTo(Cursor.cur);
                                  ^
  symbol:   variable cur
  location: class Cursor
.\john\window\component\InfiniteItemSlotPanel.java:17: error: cannot find symbol

    } else if (Cursor.cur.item.id == this.itemStack.item.id) {
                     ^
  symbol:   variable cur
  location: class Cursor
.\john\window\component\InfiniteItemSlotPanel.java:18: error: cannot find symbol

      Cursor.cur.count += this.itemStack.count;
            ^
  symbol:   variable cur
  location: class Cursor
6 errors

表示该变量不存在

我试图在这个网站(以及谷歌)上发现类似的问题,但我的大部分内容都是如此 发现是无关紧要的。在StackOverflow上,我找到了What is reflection and why is it useful?Bytecode features not avaliable in the Java languageBenefits of prototypal inheritance over classical等问题。

有问题的课程:

package john;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import john.item.*;
import john.window.*;
import john.window.component.*;

public class Cursor extends ItemStack implements MouseListener,MouseMotionListener {
  public static Cursor cur = new Cursor();

  public Cursor() {
    super(null);
  }

  public static Cursor getCursor() {
    return cur;
  }

  public void mouseClicked(MouseEvent e) {
    Object source = e.getSource();
    if (source instanceof InfiniteItemSlotPanel) {
      ((InfiniteItemSlotPanel) source).mouseClicked(e);
    } else if (source instanceof ItemSlotPanel) {
      ((ItemSlotPanel) source).mouseClicked(e);
    }
  }
  public void mouseEntered(MouseEvent e) {

  }
  public void mouseExited(MouseEvent e) {

  }
  public void mousePressed(MouseEvent e) {

  }
  public void mouseReleased(MouseEvent e) {

  }
  public void mouseDragged(MouseEvent e) {

  }
  public void mouseMoved(MouseEvent e) {

  }

  public Cursor addListenerTo(Component x) {
    x.addMouseListener(this);
    x.addMouseMotionListener(this);
    return this;
  }
}

ItemStack.java:

package john.item;

public class ItemStack {
  public Item item;
  public int count;

  public ItemStack(Item item) {
    this(item,1);
  }
  public ItemStack(Item item,int count) {
    this.item = item;
    this.count = count;
  }

  public boolean isEmpty() {
    return this.item == null && this.count == 0;
  }
  public void check() {
    if (this.count < 0) {this.count = 0;this.item = null;}
    if (this.item == null && this.count > 0) this.count = 0;
    if (this.count == 0 && this.item != null) this.item = null;
  }
  public void dispose() {
    this.item = null;
    this.count = 0;
  }
  public boolean canInsert(ItemStack stack) {
    return this.item.id == stack.item.id;
  }
  public void insert(ItemStack stack) {
    if (!canInsert(stack)) return;
    this.count += stack.count;
  }
  public void insertInto(ItemStack dest) {
    dest.insert(this);
  }
  public void copyTo(ItemStack dest) {
    dest.item = this.item;
    dest.count = this.count;
  }
}

如果你需要更多的代码,请不要犹豫,我会尽力压缩它,但它目前包含14个互锁类,所以我不知道会有多好。< / p>

我不确定这个问题会有多好,但我不知道还能去哪儿。我道歉 如果我浪费时间,但我试图使问题符合StackOverflow的指导方针。任何回复都是赞赏的(甚至是负面的)。我真的需要弄清楚为什么会这样。最后一次 我有一个与此类似的问题,我不得不删除并重写所有内容,所以任何信息都会很棒!还有,对不起我的英语很差(我是美国出生和长大的,但我不经常写这种东西了。)

0 个答案:

没有答案