Access object created in one class into another

2019-02-23 07:05发布

I have a primary class as below:

public class classB{

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

The classC has a contructor:

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 classA which will be using the object created in classB(which is of course, an instance of 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 classB and this would be further used in classC's methods. Creating a new object will render my variable in classB set to null which isn't intended.

How can I achieve this?

2条回答
Ridiculous、
2楼-- · 2019-02-23 07:45

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

But the Brand needs access the container it belongs to it could be done with the mapping but we are simply inject the Run to the Brand so it's better implement the Runable or annotate it with JSR330. And accessing the container through the Run in the normal way.

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);
  }
}
查看更多
啃猪蹄的小仙女
3楼-- · 2019-02-23 08:02

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

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

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.

查看更多
登录 后发表回答