CODEARTS IDE-用工厂方法替换构造函数:重构后

时间:2024-04-16 15:05:23

重构后

class ReplaceConstructor {
   public static void main(String[] args) {
      InnerClass.newInnerClass("Hello", "World").print();
   }

   private static class InnerClass {
      private String message;

      private InnerClass(String hello, String world) {
         message = hello + ", " + world;
      }

      public static InnerClass newInnerClass(String hello, String world) {
         return new InnerClass(hello, world);
      }

      public void print() {
         System.out.println(message);
      }
   }
}
support.huaweicloud.com/usermanual-codeartside/codeartside_01_0149.html