从 java 中的其他函数调用 cplex 变量

invoke cplex variable from an other function in java

我们如何调用 cplex (IloCplex cplexx = new IloCplex();) 来自其他函数的变量?因为我需要在其他函数中经常使用它,但我不能这样做,因为它的行为就像一个局部变量。

让它成为一个全局变量?

因此:

public class MyClass{
    private IloCplex cplexx;

    public MyClass(){
        cplexx = new IloCplex();
    }

    //use cplexx anywhere in MyClass with this.cplexx
}

如果你想使用相同的 Cplex,通过将此方法添加到 MyClass 来创建 getter:

public IloCplex getCplexx(){
    return this.cplexx;
}

而在其他 类 中,您可以这样做:

public class Main{
    public static void main(String args[]){
        MyClass mine = new MyClass();
        mine.getCplexx().solve(); //or some other logic
        System.out.println(mine.getCplexx().getObjValue());
    }
}

希望对您有所帮助