createOperate方法不能声明为static;静态方法只能在静态或顶级类型中声明

时间:2014-10-12 10:29:58

标签: java

public class Welcome {
    public class OperationFactory {

        public static Operation createOperate(String operate) {
            Operation operation = null;
            if("+".equalsIgnoreCase(operate)) {
                operation = new OperationAdd();
            }else if("-".equalsIgnoreCase(operate)) {
                operation = new OperationSub();
            }else if("*".equalsIgnoreCase(operate)) {
                operation = new OperationMul();
            }else {
                operation = new OperationDiv();
            }
            return operation;
        }
    }
}

问题是:方法createOperate不能声明为static;静态方法只能在静态或顶级类型中声明 我不知道该如何解决。谢谢

1 个答案:

答案 0 :(得分:1)

正如错误消息明确指出的那样,您有3个选项:

  1. createOperate方法更改为非静态
  2. OperationFactory类更改为静态
  3. 将静态方法createOperate移至封闭的Welcome