0

我想知道是否有人可以让我知道如何从我在主(小程序)类中创建的对象访问我的主(小程序)类中的对象。消息来源可能会澄清一些事情。通常我会使用访问器,但这是为了简单起见

public class Bravo  {

  int copyint;

  Bravo()  {

    // Here I want to access the targetobj's theint from here
    copyint = targetobj.theint;  // I belive this doesn't work 
  }
}

public class Charlie  {

  static int theint;

  Charlie() {

    theint = 7;
  }
}

public class alpha extends JApplet {

  public void init() {

    createApp();
  }

  public void createApp()  {

     Charlie targetobj = new Charlie();
     Bravo askingobj = new Bravo();
  }
}

TYIA-罗兰

4

1 回答 1

0

看到static int theint您已将其声明为静态。所以你不需要使用对象来访问这个变量。您可以只使用className.variable,如下所示:

Charlie.theint;

如果类对其他类可见,这将起作用。

于 2013-02-25T03:42:23.797 回答