繁体   English   中英

如何通过Java使用2类中1类的方法变量?

[英]How to use variable of method from 1 class in 2 class via Java?

public static class One {

    @Override
    public String interact(String... values) {
        String actualTextOne = "test";
        return actualTextOne;
    }
}

public static class Two {

    @Override
    public String interact(String... values) {
        String actualTextTwo = "test";

        /*    Here I need to compare actualTextOne and actualTextTwo, but the problem is that I can't find solluction how to use actualTextOne in Two class*/

        return actualTextTwo;
    }
}

你不能这样做。
请检查java中的变量范围。
https://www.codecademy.com/articles/variable-scope-in​​-java


这里一个可能的解决方案是从类One调用方法interact 像这样的东西

public static class Two {

    @Override
    public String interact(String... values) {
        String actualTextTwo = "test";

        One one = new One();
        String actualTextOne = one.interact(values);
        
        // compare values here

        return actualTextTwo;
    }
}
  1. 如果您不使用它,为什么在您的类中函数有参数?

  2. 只有在嵌套时,您才能使用静态标记您的类,否则您需要这样做:

     class Two { static public String interact(String... values) { String actualTextTwo = "test"; return actualTextTwo; }

    }

  3. String textOne = One.interact(""); String textTwo = Two.interact(""); System.out.println(textOne==textTwo);

暂无
暂无

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

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