0

我在一个 java 文件中有一个 Private 方法,其中有一个名为 maxRate 的变量。我需要另一个 java 文件中的 maxRate 值来将它与另一个变量 minRate 进行比较。

如何使 maxRate 变量对另一个 java 文件可见?

我的代码如下 A.java (在 diff 包中)

public class A{

  private Integer maxRate = null;

  private modelAndView dConfig(int a,string b,string c, string d, string e){
    Map<String, PropConf> map = getConf(b,c,d,e);
    PropConf propConf = map.get(getKey(a));
    Integer maxRate = propConf.getMaxRate();
  }
}

问题:我需要位于不同包中的 B.java 中的 maxRate 值。

进步:

截至目前,根据建议,我在公共类 A{ 的顶部将 maxRate 声明为 private Integer maxRate ,并且还在 A.java 中声明了一个 getter 方法,如下所示,

public Integer getMaxRate(){
return this.maxRate
}

然后我尝试从 B.java 中调用 getMaxRate,如下所示,B.java

public ModelAndView save() {

A a = new A();
Integer f = a.getMaxRate();
logger.debug("F is" +f); }

f 值被打印为空。

4

5 回答 5

2

您无法了解该方法的内部结构。要么使其成为具有关联 getter 的类级别字段,要么从您的方法中返回它。

于 2013-02-25T08:22:55.053 回答
0

您不能从另一个方法访问方法变量,除非您在dConfig中调用 method2并将变量直接传递给 method2。

private modelAndView dConfig(){

    method2(maxRate);

}

您的问题的可能解决方案可能是以下...

您可以将变量 maxRate 设为私有类变量:

private Integer maxRate = 9;

添加公共getter方法:

public Integer getMaxRate() {
    return this.maxRate;
}

编辑更新:

您的代码应如下所示:

public class A {

  private Integer maxRate;

  public A() {
    this.maxRate = 9; //Initialize of the variable
  }

  public Integer getMaxRate() {
    return this.maxRate;
  }
}

正如我在评论中所说,您似乎缺少变量的初始化部分。

于 2013-02-25T08:26:12.473 回答
0

确实需要从头开始重新审视面向对象的编程概念。我严重怀疑,您的问题比可变可见性问题更深,而是与不正确的类结构有关......

要直接处理这种情况,无需深入,这种方法可以工作:

public class MyClass {
    //instance variable: this can be different in 
    //each instances of this class. Now we initialize it to null
    private Integer maxRate=null; 

    private modelAndView dConfig(){

        //IMPORTANT!!!!!! Don't **declare** a new variable maxRate!
        // Your code Integer maxRate = 9; **declares a new one**
        // that would hide your class variable with a enw variable, 
        // whose scope is the method only

        maxRate = 9; //this sets the instance variable
        //this only happens when running the method
        //... I assume there are quirte some code here

    }

    /**
     * This function is available for each MyClass instance, and retrieves the
     * coresponding maxRate value.
     */
    public Integer getMaxRate() { 
         return maxRate;
    }

    //... other code
}

编辑

我看到你在使用这个结构时遇到了麻烦。请听从我的建议,并阅读 OOP 的全部内容。您甚至无法坐起来就试图奔跑!

从 B 类中的函数中,您需要执行以下操作:

public ModelAndView save() {

    A a = new A(); //created class A -- initialized maxRate to null!!
    //now need to run the method that sets the instance variable:
    a.dConfig(); // maxRate set to 9, or whatever.

    //don't use single char variable names! also, f is for float, if something!
    Integer maxRate = a.getMaxRate();

    //surprise. 
    logger.debug("F is" +f); 
}

由于您没有透露您的完整代码,我只能猜测您试图实现的目标......

还有一些建议,因为您似乎需要指导:

于 2013-02-25T08:26:38.880 回答
0

您可以创建一个 getter 方法来获取该变量的值,但您需要将其声明为实例变量。

public class A {
    private Integer maxRate;

    public Integer getMaxRate() {
        return this.maxRate;
    }

    private modelAndView dConfig(){
        this.maxRate = 9;
    } 
}
于 2013-02-25T08:26:41.357 回答
0

您需要在私有函数之外定义变量并将其定义为公共。您还可以在私有函数之外定义它(保持私有),然后添加公共 getter 和/或 setter。由于其他答案中涵盖了更明显的公共获取/设置方法和公共类变量选项,因此从类中提取私有字段的方法不太常见且更复杂。首先,必须在私有函数字段之外声明变量,函数内部的函数甚至无法通过反射访问(可能是读取 Java 字节码的项目):

Field f = obj.getClass().getDeclaredField("maxRate");
f.setAccessible(true);
Integer secretInt = (Integer) f.get(obj);
于 2013-02-25T08:22:43.063 回答