2015-01-20 119 views
-1

我觉得我的问题很简单,但我不知道该怎么去Google。访问另一个对象中创建的对象

我有一个run.java基本上做到这一点

public class run { 
    //stuff 
    public static void main(String[] args) { 
     //stuff  
     Solver solver = new Solver(body,seeds); 
     solver.solve(); 
     LogTool.print("OUTPUT field of CLASS CREATED IN solver -- " + IDONTKNOWHOW,"notification"); 
    } 
} 
=========== 
public class Solver { 
    //stuff 
    public void solve() { 
     //stuff 
     GlobalState GLowestState = new GlobalState(this.Cur_state); 
    } 
} 

所以,我如何才能从run.java中访问GLowestState?

是否有区别,GLowestState是用Singleton实现的?
我希望不是。
我无法尝试任何事情,因为我的IDE给我一个GLowestState.IDONTKNOWHOW错误。

==============

更新与更多的代码:

public class run { 
public static void main(String[] args) { 

     Voxel [][][] body = new Voxel[Config.xDIM][Config.yDIM][Config.zDIM]; 
     //stuff 

     Solver solver = new Solver(body,seeds);   

     LogTool.print("Initialized Solver Object!","notification"); 
     LogTool.print("Beginning Annealing...","notification"); 
     looper.solveSA(); 
     GlobalState GLS = looper.getGLowestState(); <--- NPE here 
     LogTool.print("GLC: " + looper.getGlobal_lowest_cost()+ " CURC: " + looper.getCur_cost(),"notification"); 
//  LogTool.print("GLS external: " + GLS,"notification"); 
     LogTool.print("SolveSA: Global Current Best Solution : " + looper.getGlobal_Lowest_state_string(),"notification"); 

========= 
public class Looper { 
    public static Voxel [][][] body; //Thobi hat das als ganz einfache Variable in seiner Loesermethode...nicht so OOP 
    public static Seed[] seeds = new Seed[Config.SAnumberOfSeeds]; 
    public double[] Cur_state = new double[Config.SAnumberOfSeeds]; 
    public double[] New_state = new double[Config.SAnumberOfSeeds]; // Do I even need this ? 
    public double[] Global_Lowest_state = new double[Config.SAnumberOfSeeds]; // Do I even need this ? 
    GlobalState GLowestState; 

    public Looper(Voxel [][][] body, Seed[] seeds) { 
     this.temperature = Config.StartTemp; 
     this.body = body; 
     this.seeds = seeds; 
       this.Cur_cost = Cur_cost; 
       this.New_cost = New_cost; 
       this.temperature = temperature; 
       this.Cur_state = Cur_state; 
       this.New_state = New_state; 
       this.Global_lowest_cost = Double.MAX_VALUE; 
       this.Global_Lowest_state = Global_Lowest_state; 
    } 

    public GlobalState getGLowestState() { 
     return GLowestState; 
    } 



     for (int ab = 0; ab < Config.NumberOfMetropolisResets; ab++) { 
      LogTool.print("==================== START CALC FOR OUTER ROUND " + ab + "=========================","notification"); 

      if (Config.SAverboselvl==1) { 
       LogTool.print("SolveSA: Cur_State Read before Metropolis : A)" + Cur_state[0] + " B) " + Cur_state[1] + " C) " + Cur_state[2],"notification"); 
       LogTool.print("Debug: GLS get 1: " + this.getGlobal_Lowest_state_string(),"notification"); 
      } 

      if (ab==0){ 
       this.initState(); 

       if (Config.SAverboselvl==1) { 
        LogTool.print("SolveSA: Cur_state after Initstate : A)" + Cur_state[0] + " B) " + Cur_state[1] + " C) " + Cur_state[2],"notification"); 
       } 
      } 

      setCur_cost(cost()); 

      /* [Newstate] with random dwelltimes */ 
      newState(); 
      if (Config.SAverboselvl==1) { 
       LogTool.print("SolveSA: New State before Metropolis: A)" + New_state[0] + " B) " + New_state[1] + " C) " + New_state[2],"notification"); 
      } 

      setNew_cost(cost()); 

      if (Config.SAverboselvl==1) { 
       LogTool.print("SolveSA: New Cost : " + New_cost,"notification"); 
      } 

      double random_double = RandGenerator.randDouble(0.01, 0.99); 

      /** 
       * MetropolisLoop 
       * @param Config.NumberOfMetropolisRounds 
      */ 

      for(int x=0;x<Config.NumberOfMetropolisRounds;x++) { 
    //   break; 
    //   LogTool.print("SolveSA Iteration " + x + " Curcost " + Cur_cost + " Newcost " + New_cost,"notification"); 
       if ((Cur_cost - New_cost)>0) { // ? die Kosten 

        if (Config.SAverboselvl>1) { 
         LogTool.print("Fall 1","notification"); 
        } 

        if (Config.SAdebug) {      
          LogTool.print("SolveSA: Metropolis NewCost : " + this.getNew_cost(),"notification"); 
          LogTool.print("SolveSA: Metropolis CurCost : " + this.getCur_cost(),"notification"); 
          LogTool.print("SolveSA Cost delta " + (Cur_cost - New_cost) + " ","notification"); 
        } 
          Cur_state = New_state; 
          Cur_cost = New_cost; 

        } else if (Math.exp(-(Cur_cost - New_cost)/temperature)> random_double) { 

         Cur_state = New_state; 
         Cur_cost = New_cost; 

         if (Config.SAdebug) { 
          LogTool.print("SolveSA: NewCost : " + this.getNew_cost(),"notification"); 
          LogTool.print("SolveSA: CurCost : " + this.getCur_cost(),"notification"); 
         } 

         if (Config.SAverboselvl>1) { 
          LogTool.print("Fall 2: Zufallsgenerierter Zustand traegt hoehere Kosten als vorhergehender Zustand. Iteration: " + x,"notification"); 
         } 
        } 

       temperature = temperature-1; 
       if (temperature==0) { 
        break; 
       } 

       random_double = RandGenerator.randDouble(0.01, 0.99); 
       newState(); 
       setNew_cost(cost()); 
      } 

      if (ab==9) { 
       double diff=0; 
      } 

//This is where the trouble happens - GlobalLoewst cost is set correctly and kept throughout the loops, GLowestState is always the last value of Cur_State (the most recent completed iteration. If smoothly running, that would be iteration 9 and inner iteration 99) @stackexchange 

      if (Cur_cost<Global_lowest_cost) { 
       this.setGlobal_lowest_cost(Cur_cost); 
       GlobalState GLowestState = new GlobalState(this.Cur_state); 
       LogTool.print("GLS DEDICATED OBJECT STATE OUTPUT -- " + GLowestState.getGlobal_Lowest_state_string(),"notification"); 
       this.setGlobal_Lowest_state(GLowestState.getDwelltimes()); 
       LogTool.print("READ FROM OBJECT OUTPUT -- " + this.getGlobal_Lowest_state_string(),"notification"); 
//    LogTool.print("DEBUG: CurCost direct : " + this.getCur_cost(),"notification");   
//    LogTool.print("Debug: Cur<global CurState get : " + this.getCur_state_string(),"notification"); 
//    LogTool.print("Debug: Cur<global GLS get : " + this.getGlobal_Lowest_state_string(),"notification"); 
//    this.setGlobal_Lowest_state(this.getCur_state(Cur_state)); 
//    LogTool.print("Debug: Cur<global GLS get after set : " + this.getGlobal_Lowest_state_string(),"notification");   
      } 
      LogTool.print("SolveSA: Iteration : " + ab,"notification"); 
      LogTool.print("SolveSA: Last Calculated New State/Possible state inner loop 99 : " + this.getNew_state_string(),"notification"); 
//   LogTool.print("SolveSA: Best Solution : " + this.getCur_state_string(),"notification"); 
      LogTool.print("SolveSA: GLS after: " + this.getGlobal_Lowest_state_string(),"notification"); 
      LogTool.print("SolveSA: NewCost : " + this.getNew_cost(),"notification"); 
      LogTool.print("SolveSA: CurCost : " + this.getCur_cost(),"notification");   
     } 
    } 

================= 

public final class GlobalState implements Comparable<Object>{ 
    private double[] dwelltimes; 
    private static GlobalState instance = null; 

    protected GlobalState(){ 
     // Exists only to defeat instantiation 
    } 

    public static GlobalState getInstance(){ 
     if (instance==null){ 
      instance = new GlobalState(); 
     } 
     return instance; 
    } 

    public GlobalState(double[] dwelltimes) { 
     this.dwelltimes = dwelltimes; 
    } 

    @Override 
    public int compareTo(Object o) { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 

    public double[] getDwelltimes() { 
     return dwelltimes; 
    } 

    public String getGlobal_Lowest_state_string() { 
     String Global_Lowest_state_string = new String(); 
     for (int cc = 0; cc < Config.SAnumberOfSeeds; cc++) { 
      Global_Lowest_state_string = Global_Lowest_state_string.concat(" " + cc + ") " + dwelltimes[cc]); 
      } 
     return Global_Lowest_state_string; 
    } 

    public void setDwelltimes(double[] dwelltimes_x) { 
     this.dwelltimes = dwelltimes_x; 
    } 

} 
+0

的'范围得到它GLowestState'仅限于目前的解决()方法。 – 2015-01-20 20:06:44

+0

我的答案如何在丹尼尔之下?你需要使用'public'属性。 – 2015-01-20 20:34:47

+0

我想提供更多的上下文。我原本有一个GlobalLowestState字段(简洁的GLS)。如果满足某个条件,我写信给该字段 - 这是在solve()方法中检查的。 问题是GLS取了最后计算的Cur_State的值而不是全局lpowest状态的值(这是检查的目的)。我发现代码没有错,所以我将全局最低值写入对象GLS而不是字段。奇迹般地,GLS对象包含了正确的值。但是,即使使用get(),它也不能在solver()类之外访问。 – 2015-01-21 16:43:43

回答

0

如果GLowestStateSingleton那么你真的不能调用它new - - 假设它是一个执行得很好的Singleton。

如果顺利实现,那么你怎么称呼它是这样的:

GLowestState.getInstance(); 

又如:

GLowestState.getInstance().foo(); 
+0

我的代码失败,出现空指针异常。您看到下面的方式成功运行并输出 - > GLS外部:[email protected](CTRL + K无法正常工作):访问其中的方法或字段失败。为什么? solver.solve();全局状态GLSW = GlobalState.getInstance(); LogTool.print(“GLS external:”+ GlobalState.getInstance(),“notification”); – 2015-01-22 18:56:15

0

可以做这样的事情:

public class Solver { 
    private GlobalState GLowestState; 

    public void solve() { 
     GLowestState = new GlobalState(this.Cur_state); 
    } 

    public GlobalState getGLowestState() { 
     return GLowestState; 
    } 
} 
+0

我想你的意思是'公共GlobalState getGLowestState()',类型。 – bcsb1001 2015-01-20 20:11:45

+0

是的。更正,谢谢。 – 2015-01-20 20:15:02

0

如果你想求解成为一个单身你需要一个私人的构造函数和一个newInstance方法。 吸气剂

public class run { 
    //stuff 
    public static void main(String[] args) { 
     //stuff 
     Solver solver = Solver.getInstance().solve(body,seeds); 
     LogTool.print("OUTPUT field of CLASS CREATED IN solver -- " + solver.getGLowestState(),"notification"); 
    } 
} 
=========== 
public class Solver { 

    private GlobalState gLowestState; 
    private static Solver singeltonSolver; 

    private Solver() { 

    } 

    public synchronized static Solver getInstance(){ 
     if(singeltonSolver== null) 
      singeltonSolver = new Solver(); 
     return singeltonSolver; 
    } 

    public void solve(SOmeType body,SOmeType seeds) { 
     //stuff 
     GlobalState GLowestState = new GlobalState(this.Cur_state); 
    } 

    public GlobalState getGLowestState() { 
     return gLowestState; 
    } 
} 
+0

在Java中,至少它们被称为字段,而不是属性。 – bcsb1001 2015-01-20 20:14:11

0

您需要实现该对象的getter类的内部创建一个域gloweststate和访问,以及由可变的instance variable,而不是一个local variable。为了您的代码,一个例子是:

public class Solver { 
    // stuff 
    GlobalState GLowestState; 

    public void solve() { 
     // stuff 
     GLowestState = new GlobalState(this.Cur_state); 
    } 
    public GlobalState getGLowestState(){ 
     return GLowestState; 
    } 
} 

注意如何GLowestState现在属于类的实例,而不是在方法solve()一个局部变量。现在,您可以拨打

Solver solver = new Solver(body,seeds); 
solver.solve(); 
GlobalState GS = solver.getGLowestState(); 

和可变GS将继续GlobalState变量为Solver对象的特定实例。

+0

这是我原本想实现的。我使用getter在GlobalState中获得了一个double []字段。然而,GlobalState不可见...... – 2015-01-21 15:33:59

+0

如果您不想使'GlobalState'可见(因为它是一个独立的类,您想在'Solver'类之外使用),您可以更改您的方法分别获取'GlobalState'对象的详细信息。例如,如果你在'GlobalState'中有一个'int'和一个'String',你可以创建一个'getGlobalStateString()'方法来返回'int'的'String'和'int'方法。就像我之前所说的那样,创建另一个类文件并使其可见,如果它将在当前类之外使用,那么这是一个很好的编程。 – 2015-01-21 15:38:40

+0

我实现了这个功能,但是当试图使用solver.getGLowestState时,我得到了一个nullpointerexception。我认为当solveSA完成时,对象被破坏..或者idk ... – 2015-01-21 15:54:01

0

我会做的是有GlobalState持有单身人士。

public enum GlobalState { 
    INSTANCE; 

    public String getValue() { ... } 
} 

但是,它是更好的Solver保持与全局状态的状态,而不是混乱。

public class Solver { 
    final String field; 
    //stuff 
    public Solver(int body, int seeds) { 
     //solve stuff here 
     this.field = "value"; 
    } 
} 

public class Run { 
    //stuff 
    public static void main(String[] args) { 
     //stuff 
     Solver solver = new Solver(body,seeds); 
     LogTool.print("OUTPUT field of " + solver.field); 
    } 
} 
+0

我读过使用枚举是,很好,实施k单身的风格。你为什么使用它? – 2015-01-21 16:23:27

0

它可以在许多方面accomplisched:

1-如果GlowestState尤其涉及solve()才返回它,并且返回的对象使用。

public GlobalState solve() { 
    GlobalState globalState = new GlobalState(this.Cur_state); 
    return globalState; 
} 

现在,您可以访问它为:

Solver solver = new Solver(body,seeds); 
GlobalState globalState = solver.solve(); 

2 - 让GlowestState一个实例,并把它公开。

public GlobalState glowestState; 
public void solve() { 
    GLowestState = new GlobalState(this.Cur_state); 
} 

然后你就可以通过调用solver.glowestState

3-使用一个getter得到GLowestState得到它。

private GlobalState glowestState; 
public void solve() { 
    glowestState = new globalState(this.Cur_state); 
} 

public getGlowestState() { 
    return GLowestState; 
} 

现在,您可以通过调用getGlowestState()