代码之家  ›  专栏  ›  技术社区  ›  user405398

如何从不同的方法访问在某个方法中创建的对象?

  •  0
  • user405398  · 技术社区  · 15 年前

    我想从同一类的其他方法访问在类的某个方法中创建的对象。

    public class SomeClass implements SomeInterface{
    
    public void someMethod(int a, int b){ //Implemented method
    {
     SomeOtherClass soc = new SomeOtherClass();
    //some setting process
    }
    
    public int someOtherMethod(){
    SomeOtherClass newSOC = soc;//Here, can i access the soc object created in "someMethod"
    }
    }
    

    实际上,我想在对象中设置一些数据并从不同的方法检索数据。有可能吗!!!

    5 回复  |  直到 15 年前
        1
  •  1
  •   mhaller    15 年前

    给定对象引用的约束 SomeClass 用于多个请求(例如,其中一个请求同时调用两个方法),并且必须正确封装它们,例如,在多用户系统中,需要有一个state对象,以便可以将第一个方法的结果传输到第二个方法。

    因为对象实例 某个阶级 在这个场景中被认为是一个共享类,您需要其他东西。

    两种方式:

    • 引入一个可以从第一个方法传递到第二个方法的状态对象(例如,使第一个方法返回对象并将其作为参数传递给第二个方法)
    • 引入其他调用方看不到的内部状态。如果您不知道自己在做什么,并且这在很大程度上取决于使用对象的上下文,那么这可能会有风险。

    两个选项都需要更改第一个方法的实现 .

    如果由于任何原因,您无法更改第一种方法的实现,那么您的问题的答案是: 不,你不能 访问 soc 对象,因为第一个对象超出范围,例如,它可能已被垃圾收集并消失。

    对于后者,您可能需要使用 ThreadLocal . 如果您知道请求(例如HTTP Servlet请求)对于用户或请求是唯一的,因此可以在 螺纹局部 在调用第一个方法和第二个方法之间。如果您反对 某个阶级 是被不同的线程重用的,那么你需要好好清理一下!

        2
  •  2
  •   Nathan Hughes    11 年前

    从创建它的方法返回它。或者使对象成为类的实例成员。不知道上下文很难说。

    ThreadLocal有一些优点(例如请求授权信息、连接和Spring中的事务),但它也可能是一个非常肮脏的黑客。最好通过参数显式地传递信息,这样可以更清楚地了解发生了什么。

        3
  •  0
  •   Eternal Noob    15 年前

    创建一个全局对象,并使该对象引用新创建的对象,因此任何方法都可以访问它。

    public Object global_object;
    
    void a(){
    
    Object aObj = new Object();
    global_object = aObj;
    
    
    }
    
    
    void b(){
    //use global_object here.
    }
    
        4
  •  0
  •   z00bs    15 年前

    通过声明一个字段并将新创建的对象分配给它,或者将创建的对象传递给第二个方法。

    分配字段:

    public class SomeClass implements SomeInterface {
        private SomeOtherClass fSoc;
    
        public void someMethod(int a, int b) {
           fSoc = new SomeOtherClass();
           fSoc.setSomeField("w000t");
        }
    
        public int someOtherMethod() {
            // access here the field fSoc but keep in mind
            // that it might not have been initialized
            if (fSoc != null) {
                String someData = fSoc.getSomeField();
            }
        }
    }
    
    public class SomeOtherClass {
        private String fSomeField;
    
        public void setSomeField(String field) {
            fSomeField = field;
        }
    
        public String getSomeField() {
            return fSomeField;
        }
    }
    
        5
  •  0
  •   Telmo Marques    15 年前

    如果你想要一个对象被一个类的所有方法访问,那么我认为zOObs的方法是正确的。通过在类中声明私有字段,该字段将由该类中的所有方法可见。

    这段代码是z00bs的方法,仅提供一些详细信息:

    public class SomeClass implements SomeInterface {
        //Here we declare the private field, visible by all this class. It's value is null
        private SomeOtherClass fSoc;
    
        public void someMethod(int a, int b) {
           /*Because we declared a private field named fSoc, it can be accessed by all methods of this class.
           Here we assign it a new instance of another class. It's value is no longer null.*/
           fSoc = new SomeOtherClass();
        }
    
        public int someOtherMethod() {
            /*In this method access the fSoc field, that already has been initialized by someMethod().
            But as z00bs's said, keep in mind that if someMethod() wasn't already called, then fSoc isn't initialized (it's value could be null)*/
            if(fSoc != null)
                //If fSoc is not null, do something...
                String someVariable = fSoc.someMethod();
        }
    }