java 访问在一个类中创建的对象到另一个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10570393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:36:52  来源:igfitidea点击:

Access object created in one class into another

javaclassobject

提问by curlyreggie

I have a primary class as below:

我有一个初级课程如下:

public class classB{

  public classC getObject(String getstring){
     return new classC(getstring);
    }
}

The classChas a contructor:

classC有一个构造器:

public class classC{

 String string;

 public classC(String s){
  this.string = s;
 }

 public methodC(int i){
   <using the `string` variable here>
 }
}

Now I've a classAwhich will be using the object created in classB(which is of course, an instance of classC).

现在我有一个classA将使用在中创建的对象classB(当然,它是 的一个实例classC)。

public classA{
  int a = 0.5;

  <Get the object that was created in classB>.methodC(a);

}

This is needed as a variable is created on some actions from the user and stored in classBand this would be further used in classC's methods. Creating a new object will render my variable in classBset to null which isn't intended.

这是必需的,因为在用户的某些操作上创建了一个变量并将其存储在 中classB,这将在classC的方法中进一步使用。创建一个新对象会将我的变量classB设置为 null,这不是预期的。

How can I achieve this?

我怎样才能做到这一点?

回答by Roman C

Assume the Brandis a lightweight objects and Runis heavyweight then creating a field with the container for the lightweight objects and hiding it is a good idea.

假设这Brand是一个轻量级对象并且Run是重量级的,那么为轻量级对象创建一个包含容器的字段并将其隐藏是一个好主意。

But the Brandneeds access the container it belongs to it could be done with the mapping but we are simply inject the Runto the Brandso it's better implement the Runableor annotate it with JSR330. And accessing the container through the Runin the normal way.

但是Brand需要访问它所属的容器可以通过映射来完成,但我们只是简单地将 注入Run到 中,Brand因此最好Runable使用 JSR330实现或注释它。并通过Run正常方式访问容器。

class MainClass {
  public static void main(String[] args) {
    Run r = new Run();
  }
}

class Run {
  private Container con1 = new Container();

  public Run() {
    Brand cola = new Brand("Coca Cola");
    Brand pepsi = new Brand("Pepsi");

    // Creates the container object "con1" and adds brands to container.
    add(cola);
    add(pepsi);
  }
  public void add(Brand b){
    con1.addToList(b);
    b.setRun(this);
  }

  public Container getContainer() {
    return con1;
  }
}

class Brand {
  // In this class I have a method which needs to accsess the con1 object
// containing all the brands and I need to access the method
  private String name;
  private Run run;

  public Brand(String name){
    this.name = name;

  }
  public void brandMethod() {
    if(getRun().getContainer().methodExample()) {        // Error here. Can't find "con1".**
      System.out.println("Method example returned true.");
    }
  }


  public Run getRun() {
    return run;
  }

  public void setRun(Run run) {
    this.run = run;
  }
}

class Container {
  // This class is a container-list containing all brands brands
  private ArrayList<Object> list = new ArrayList<Object>();

  public boolean methodExample(){
    return false;
  }

  public void addToList(Object o) {
    list.add(o);
  }
}

回答by Arijoon

If you want to get the object created in classB a static field should do the job

如果你想获得在 classB 中创建的对象,静态字段应该完成这项工作

public class classB {

  public static objectCReference;

  public classC getObject(String getstring){
     objectCReference =  new classC(getstring);
     return objectCReference;
  }
}

Then you can access the reference in A

然后你可以访问A中的引用

public classA {
  int a = 0.5;

  if (classB.objectCReference != null) { // Make sure to null check 
    classB.objectCReference.methodC(a); 
  }

}

Also please follow the language conventions and start your class names with capital letters.

此外,请遵循语言约定并以大写字母开头您的班级名称。