Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从内部类访问父小程序_Java_Oop_Applet - Fatal编程技术网

Java 从内部类访问父小程序

Java 从内部类访问父小程序,java,oop,applet,Java,Oop,Applet,我想知道是否有人可以让我知道如何从我在主(applet)类中创建的对象访问主(applet)类中的对象。消息来源可能会澄清一些事情。通常我会使用访问器,但这是为了简单起见 public class Bravo { int copyint; Bravo() { // Here I want to access the targetobj's theint from here copyint = targetobj.theint; // I belive this

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

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();
  }
}
蒂亚
-罗兰

请参见
静态输入
您已将其声明为静态输入。所以您不需要使用对象来访问这个变量。您只需使用
className.variable
,如下所示:

Charlie.theint;

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

我相信这不起作用。当您尝试编译时发生了什么?这是我的想法,但我真的怀疑这在当前状态下是否起作用。您可以在下面看到我的答案,这应该起作用。有没有理由不在Bravo构造函数中添加Charlie参数,然后将targetObj传递给它?好吧,我的示例是错误的,我将重新发布一些更准确地反映我的问题的内容。。。除非是这么简单。嗯,但是我必须重新发布