CODEARTS IDE-用生成器替换构造函数:重构后

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

重构后

class ReplaceConstructor {
   public static void main(String[] args) {
      new InnerClassBuilder().createInnerClass().print();
   }

   static class InnerClass {
      private String message;

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

      public void print() {
         System.out.println(message);
      }
   }
}

public class InnerClassBuilder {
   private String hello = "Hello";
   private String world = "World";

   public InnerClassBuilder setHello(String hello) {
      this.hello = hello;
      return this;
   }

   public InnerClassBuilder setWorld(String world) {
      this.world = world;
      return this;
   }

   public ReplaceConstructor.InnerClass createInnerClass() {
      return new ReplaceConstructor.InnerClass(hello, world);
   }
}
support.huaweicloud.com/usermanual-codeartside/codeartside_01_0150.html