Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 静态方法和变量的另一个问题_Java_Variables_Methods_Map_Static - Fatal编程技术网

Java 静态方法和变量的另一个问题

Java 静态方法和变量的另一个问题,java,variables,methods,map,static,Java,Variables,Methods,Map,Static,我有一个名为ManagementServiceHandler的类,它实现了一个接口。 在这个类中,有几个变量和方法,其中有一个私有静态映射和两个静态方法: public class ManagementServiceHandler implements ManagementService.Iface { private static Map<NodeManifest, Integer> nodePorts; ... other methods and variables

我有一个名为ManagementServiceHandler的类,它实现了一个接口。 在这个类中,有几个变量和方法,其中有一个私有静态映射和两个静态方法:

public class ManagementServiceHandler implements ManagementService.Iface {

  private static Map<NodeManifest, Integer> nodePorts;

  ... other methods and variables
  ...
  ...   

  public static Map<NodeManifest, Integer> getNodePorts() {
        return nodePorts;
  }


  public static void setNodePorts(Map<NodeManifest, Integer> nodePorts) {
    ManagementServiceHandler.nodePorts = nodePorts;
  } 
但我仍然需要访问充满NodeManifest对象的Map节点报告中的数据,以便使executeOperation()按预期工作

所以我的问题有两个:

  • 为什么在这种情况下不能使用static
  • 如何修改executeOperation()以使其访问 映射ManagementServiceHandler创建的节点报告(如果不是 空的

  • 这里有两个选项,要么将getter和setter保持为
    静态方法,在这种情况下,可以使用
    ClassName.methodName()

    不要使它们成为静态的,在这种情况下,您需要创建类的实例
    ManagementServiceHandler
    ,并使用
    objectName.methodName()
    调用这些方法。。。这不是一个编码决策,而是一个设计决策,您必须根据使用类
    ManagementServiceHandler
    的方式做出


    研究单例模式,也许这是最适合你的设计模式。

    你有没有试着向告诉你“在这种情况下,使用静态方法和变量是不正确的”的人问过这些问题,但是他离开了,有几天无法联系到我,我需要尽快知道这一点。也许会帮助回答你的静态问题。嗯,谢谢你的链接,但这对我没有多大帮助,这是关于静态和嵌套类的,但我在一个类中有一个静态方法,一个类必须从另一个类访问数据。。。我很困惑……好吧,我知道我需要实例化一个ManagementServiceHandler类型的对象,以便访问它的方法,但我还需要访问映射节点报告。
    class NodeManifest {
    String hostName;
    List<String> servicelist;
    }
    
    public class ManagementServiceTest {
    
     public static class ManagementServer implements Runnable {
        code for ManagementServer
     }
    
     ...
     ...
    
    public static class ArithmeticServer implements Runnable {
        code for ArithmeticServer
    }
    
    ...
    ...
    
    
      public void testArithmeticServerCompletion() throws Exception {
    
        class ArithmeticServiceDispatcher {
    
            public long executeOperation(String opName, int num1, int num2) throws TException {
                long result = 0;
                for(Map.Entry<NodeManifest, Integer> pair : ManagementServiceHandler.getNodePorts().entrySet()) {
                    List<String> operations = pair.getKey().serviceList;
                    if(operations.contains(opName)) {
                        switch(opName) {
                        case "addition":
                            result = arithmeticClient.add(num1,  num2);
                            break;
                        case "multiplication":
                            result = arithmeticClient.multiply(num1,  num2);
                            break;
                        case "substraction":
                            result = arithmeticClient.substract(num1,  num2);
                            break;
                        case "division":
                            result = arithmeticClient.divide(num1,  num2);
                            break;
                        }
                    break;
                    }
                }
            return result;
            }
        }
    
    ManagementServiceHandler.getNodePorts() 
    
                                       OR