简体   繁体   English

如何将Object从java类传递给另一个java类

[英]How to pass Object from java class to another java class

I created an instance of a class in java as following: 我在java中创建了一个类的实例,如下所示:

ABC ab = new ABC();

I want to access this instant ab in another class XYZ . 我想在另一个类XYZ访问此即时ab How to make this Object available in class XYZ? 如何在XYZ类中使用此对象?

It is difficult to answer your question without more specific information about your problem, but this would certainly work: 如果没有关于您的问题的更具体信息,很难回答您的问题,但这肯定会奏效:

You can use a setter to initialize an instance variable in your other class if you want to use it everywhere in that class: 如果要在该类中的任何位置使用它,可以使用setter在其他类中初始化实例变量:

class someClass {
   void someMethod() {
      ABC ab = new ABC();
      XYZ xyz = new XYZ();
      xyz.setABC(ab);
   }
}

class XYZ {
   ABC ab;
   //...
   void setABC(ABC ab) {
     this.ab = ab;
   }
   //...
   void doSomething() {
      // do something with instance variable ab
   } 
}

There are several ways to do what you want to achieve. 有几种方法可以实现您想要实现的目标。 Some of them might be as follows: 其中一些可能如下:

Passing the object reference through a constructor 通过constructor传递对象引用
Here, you would explicitly pass the reference of your reference class when you're creating an object of the actual class. 在这里,当您创建实际类的对象时,您将显式传递引用类的引用。

public class ActualClass{
    private ReferenceClass refClassObject;

    /**
    * Passing through a constructor
    */
    public ReferenceClass(ReferenceClass refClassObject){
        this.refClassObject = refClassObject;
    }
}

class ReferenceClass{
    /**
    * Your implementation of the class
    */
}

Using getter/setter methods 使用getter / setter方法
In this approach, you would pass the reference of your object through explict public setXX() methods. 在这种方法中,您将通过explict public setXX()方法传递对象的引用。 This approach is more flexible because you can update the reference object as and when you want to (think polymorphism ). 这种方法更灵活,因为您可以根据需要更新引用对象(想想polymorphism )。 As an example: 举个例子:

public class ActualClass{
    private ReferenceClass refClassObject;

    public ActualClass(){
    }

    public void setReferenceClass(ReferenceClass refClassObject){
        this.refClassObject = refClassObject;
    }

    public ReferenceClass getReferenceClass(){
        return refClassObject;
    }
}

class ReferenceClass{
    /**
    * Your implementation of the class
    */
}

Using a combination of constructors and getters/setters 使用构造函数和getter / setter的组合
For added flexibility, you might want to initialize your Actual class object with a reference. 为了增加灵活性,您可能希望使用引用初始化Actual类对象。 However if you would also want to keep the option of changing the reference at object at a later stage, go for a combination of both #1 & #2 that I specified above. 但是,如果您还希望保留在稍后阶段更改对象引用的选项,请选择我在上面指定的#1和#2的组合。

public class ActualClass{
    private ReferenceClass refClassObject;

    public ActualClass(){
    }

    public ActualClass(ReferenceClass refClassObject){
        this.refClassObject = refClassObject;
    }

    public void setReferenceClass(ReferenceClass refClassObject){
        this.refClassObject = refClassObject;
    }

    public ReferenceClass getReferenceClass(){
        return refClassObject;
    }
}

class ReferenceClass{
    /**
    * Your implementation of the class
    */
}

Which one should you choose? 你应该选择哪一个? Well it would depend on your implementation and requirement. 那么这取决于你的实施和要求。

This answer is exactlty same as Doug Ramsey this link I tried to explain with the same logic. 这个答案与Doug Ramsey完全相同, 这个链接我试图用相同的逻辑来解释。

  public class A {
        public void m1() {
            System.out.println("inside m1 method");
            ABC ab = new ABC(); // 2 object is made and reference is given to ab 
            XYZ xyz = new XYZ();  3 object is made and reference is given to xyz 
            xyz.send(ab); // 4 THIS IS WHAT YOUR QUESTION MEANS
        }
    }


    class XYZ {
       ABC ab;
       //...
       void send(ABC ab) { // 5
         this.ab = ab;
         System.out.println("inside send");
         callme(); 
       }
       //...
       void callme() { // 6
           System.out.println("A is : "+ab.a);
           System.out.println("b is : "+ab.b);
          // do something with instance variable ab
       } 
    }

public class ABC {
    int a = 10;
    static int b= 20;
    public static void main(String[] args) // called first
    {
        A a = new A();
        a.m1();
    }
}

You have two ways to pass object parameter to one class to another. 您有两种方法可以将对象参数传递给另一个类。

  • Passing parameter to a method 将参数传递给方法

     public void passMethod(ABC ab) { } 
  • Passing parameter to a constructor 将参数传递给构造函数

     public class XYZ { public XYZ(ABC ab) { } } 

I know this question is old, but If I'm correct you want to transfer an Object into another class to be used. 我知道这个问题很老,但是如果我是正确的,你想将一个Object转移到另一个要使用的类中。

In order to do that you need a few things 为了做到这一点,你需要一些东西

Class XYZ has to have a constructor to take in the parameter "Object" it would something like 类XYZ必须有一个构造函数来接受参数“Object”它会是什么样的

class XYZ{
    private Object ab
    public XYZ(Object ab){
        this.ab = ab;//This is the constructor called when you create an XYZ object, and want to use the Object ab in XYZ
    }
package demo;

class ABC{
     void disp(xyz arg1){
        System.out.println("runing disp method in pratics");
        System.out.println("x value:"+arg1.x);
        arg1.test();
     }

}

class xyz{
     int x = 67;
     void test(){
        System.out.println("runing test method in pratics");
     }
}

class pratics {

    public static void main(String[] args) {
        ABC ab=new ABC();
        ab.disp(new xyz());
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM