• <small id='z4BIb'></small><noframes id='z4BIb'>

      <tfoot id='z4BIb'></tfoot>

      <legend id='z4BIb'><style id='z4BIb'><dir id='z4BIb'><q id='z4BIb'></q></dir></style></legend>
    1. <i id='z4BIb'><tr id='z4BIb'><dt id='z4BIb'><q id='z4BIb'><span id='z4BIb'><b id='z4BIb'><form id='z4BIb'><ins id='z4BIb'></ins><ul id='z4BIb'></ul><sub id='z4BIb'></sub></form><legend id='z4BIb'></legend><bdo id='z4BIb'><pre id='z4BIb'><center id='z4BIb'></center></pre></bdo></b><th id='z4BIb'></th></span></q></dt></tr></i><div id='z4BIb'><tfoot id='z4BIb'></tfoot><dl id='z4BIb'><fieldset id='z4BIb'></fieldset></dl></div>

          <bdo id='z4BIb'></bdo><ul id='z4BIb'></ul>

        如何标记一个方法是强制性的?

        时间:2023-09-28
          <bdo id='n6lpe'></bdo><ul id='n6lpe'></ul>
              <tbody id='n6lpe'></tbody>

            <i id='n6lpe'><tr id='n6lpe'><dt id='n6lpe'><q id='n6lpe'><span id='n6lpe'><b id='n6lpe'><form id='n6lpe'><ins id='n6lpe'></ins><ul id='n6lpe'></ul><sub id='n6lpe'></sub></form><legend id='n6lpe'></legend><bdo id='n6lpe'><pre id='n6lpe'><center id='n6lpe'></center></pre></bdo></b><th id='n6lpe'></th></span></q></dt></tr></i><div id='n6lpe'><tfoot id='n6lpe'></tfoot><dl id='n6lpe'><fieldset id='n6lpe'></fieldset></dl></div>

              <small id='n6lpe'></small><noframes id='n6lpe'>

            • <tfoot id='n6lpe'></tfoot>
              • <legend id='n6lpe'><style id='n6lpe'><dir id='n6lpe'><q id='n6lpe'></q></dir></style></legend>

                  本文介绍了如何标记一个方法是强制性的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  假设您使用 builder 模式创建了一个名为 Person 的类,并假设 Builder 类包含方法 body()head()arms() 当然还有 build() 并且你认为方法 head()build() 对于这个类的用户来说是必须的.

                  Suppose you create a class names Person using the builder pattern, and suppose the Builder class contains methods body(), head(), arms() and of course build() and you consider methods head() and build() obligatory for the user of this class.

                  我们希望以某种方式将这些方法标记为强制性,如果可能的话,使用注释.如果这个类的用户试图构建一个 Person 实例但忘记调用这些方法中的任何一个,我们希望得到某种警告 - 来自 java 编译器,或者可能来自我们用来构建我们的 Eclipse 或 Maven项目 - 他们中的任何一个都可以.

                  We would like to somehow mark these methods obligatory, if possible using annotations. If a user of this class tries to build a Person instance but forgot to call either of these methods, we would like to get some kind of warning - either from the java compiler, or maybe from Eclipse or Maven, which we use to build our projects - any of them would do.

                  有可能吗?你会建议哪种方式?

                  Is it possible to do? Which way would you suggest?

                  推荐答案

                  这是一个使用不同类型强制某些部分的示例(它也强制调用方法的顺序):

                  Here is an example with using different types to make some parts mandatory (it also makes the order you call the methods mandatory):

                  package test;
                  
                  import test.StepOne.StepThree;
                  import test.StepOne.StepTwo;
                  import test.StepOne.LastStep;
                  
                  public class TestBuilder {
                  
                      public static void main(String[] args) {
                  
                          String person1 = PersonBuilder.newInstance().head("head").body("body").arm("arm").leg("leg").build();
                  
                          String person2 = PersonBuilder.newInstance().head("head").body("body").arm("arm").build();
                  
                      }
                  
                  }
                  
                  interface StepOne {
                  
                      // mandatory
                      StepTwo head(String head);
                  
                      interface StepTwo {
                          // mandatory
                          StepThree body(String body);
                      }
                  
                      interface StepThree {
                          // mandatory
                          LastStep arm(String arm);
                      }
                  
                      // all methods in this interface are not mandatory
                      interface LastStep {
                          LastStep leg(String leg);
                          String build();
                      }
                  
                  }
                  
                  class PersonBuilder implements StepOne, StepTwo, StepThree, LastStep {
                  
                      String head;
                      String body;
                      String arm;
                      String leg;
                  
                      static StepOne newInstance() {
                          return new PersonBuilder();
                      }
                  
                      private PersonBuilder() {
                      }
                  
                  
                  
                      public StepTwo head(String head) {
                          this.head = head;
                          return this;
                      }
                  
                      public LastStep arm(String arm) {
                          this.arm = arm;
                          return this;
                      }
                  
                      public StepThree body(String body) {
                          this.body = body;
                          return this;
                      }
                  
                      public LastStep leg(String leg) {
                          this.leg = leg;
                          return this;
                      }
                  
                      public String build() {
                          return head + body + arm + leg;
                      }
                  }
                  


                  编辑

                  OP 对这个答案印象深刻,他将其完整地写在 博客.这是对构建器模式的巧妙理解,值得在这里引用完整的处理方法.

                  The OP was so impressed with this answer that he wrote it up fully in a blog. It's such a clever take on the builder pattern that a full treatment deserves to be referenced here.

                  这篇关于如何标记一个方法是强制性的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:与 xml 配置文件相比,注释(非编译器)的优点/缺点 下一篇:@autowired on 方法在 Spring

                  相关文章

                  最新文章

                      <bdo id='FNhbl'></bdo><ul id='FNhbl'></ul>
                    <legend id='FNhbl'><style id='FNhbl'><dir id='FNhbl'><q id='FNhbl'></q></dir></style></legend>
                    <i id='FNhbl'><tr id='FNhbl'><dt id='FNhbl'><q id='FNhbl'><span id='FNhbl'><b id='FNhbl'><form id='FNhbl'><ins id='FNhbl'></ins><ul id='FNhbl'></ul><sub id='FNhbl'></sub></form><legend id='FNhbl'></legend><bdo id='FNhbl'><pre id='FNhbl'><center id='FNhbl'></center></pre></bdo></b><th id='FNhbl'></th></span></q></dt></tr></i><div id='FNhbl'><tfoot id='FNhbl'></tfoot><dl id='FNhbl'><fieldset id='FNhbl'></fieldset></dl></div>
                      <tfoot id='FNhbl'></tfoot>
                    1. <small id='FNhbl'></small><noframes id='FNhbl'>