代码之家  ›  专栏  ›  技术社区  ›  Sajal Dutta

setter方法必须有一个参数吗?

  •  5
  • Sajal Dutta  · 技术社区  · 16 年前

    我通过ftp服务器从客户端获取值。有时这些文件包含垃圾值。例如,像#3432838#9这样的电话号码。因此,在设置值之前,我需要删除这些垃圾字符。我可以用setter方法来做吗?这是一种有效的方法吗?

    提前多谢!

    编辑:

    public void setSomething(String strValue){ 
         if(checkValidity(strValue)){ 
             // set the value 
         } else { 
             // set the value to an empty string
         }  
      }
    
    4 回复  |  直到 11 年前
        1
  •  12
  •   Community CDub    8 年前

    这在JavaBean框架模型中是特别必要的,但通常不是强制性的。

    当setter要“swith”一个值时,可以使用不带参数的setter。

    void setCheck()
    

    例如,可以将“check”布尔属性设置为true。

    因此,即使它不是JavaBean意义上的“setter”,您也可以想象setter用于其他目的。

    不止一个论点 ,例如索引属性(索引属性支持一系列值。每当读取或写入属性时,您只需指定一个索引来标识所需的值。)

    void setter(int index, PropertyType value); // indexed setter
    void setter(PropertyType values[]); // array setter
    

    在你的情况下,一个有效的方法是 runtime exception
    这样,就不会对已经调用setter的所有其他类进行任何不必要的编译时异常检查。

    或者你可以考虑你的财产 约束性质 并添加一个非运行时异常。

    需要受约束的属性设置器方法来支持PropertyVetoException。 此文档向受约束属性的用户说明可能会尝试更新 否决。 因此,简单的受约束属性可能如下所示:

    PropertyType getFoo();
    void setFoo(PropertyType value) throws PropertyVetoException;
    


    this question ):

    • 验证应该与getter或setter分开捕获
    • 最好是 (因此我建议为setter添加异常)。
        2
  •  5
  •   Marko    16 年前

    Setter完全可以“清理”其参数,如果无效,则抛出异常。

        3
  •  2
  •   Mnementh    16 年前

    如果您认为其他代码应该进行验证,但不应该设置错误的值,那么也可以在setter中使用断言。

        4
  •  1
  •   user2427    16 年前

    Joshua Bloch(ISBN-13:978-0-321-35668-0)在《有效Java第二版》一书中说,对于对象创建,最好使用构建器模式,而不是bean约定。

    例如(bean模式):

    NutritionFacts cocaCola = new NutritionFacts();
    cocaCola.setServingSize(240);
    cocaCola.setServings(8);
    cocaCola.setCalories(100);
    cocaCola.setSodium(35);
    cocaCola.setCarbohydrate(27);
    

    生成器模式的用法:

    NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8).
       calories(100).
       sodium(35).
       carbohydrate(27).
       build();
    

    生成器模式的实现:

    // Builder Pattern
    public class NutritionFacts {
        private final int servingSize;
        private final int servings;
        private final int calories;
        private final int fat;
        private final int sodium;
        private final int carbohydrate;
        public static class Builder {
            // Required parameters
            private final int servingSize;
            private final int servings;
            // Optional parameters - initialized to default values
            private int calories = 0;
            private int fat = 0;
            private int carbohydrate = 0;
            private int sodium = 0;
            public Builder(int servingSize, int servings) {
                this.servingSize = servingSize;
                this.servings = servings;
            }
            public Builder calories(int val)
            { calories = val; return this; }
            public Builder fat(int val)
            { fat = val; return this; }
            public Builder carbohydrate(int val)
            { carbohydrate = val; return this; }
            public Builder sodium(int val)
            { sodium = val; return this; }
            public NutritionFacts build() {
                return new NutritionFacts(this);
            }
        }
        private NutritionFacts(Builder builder) {
            servingSize = builder.servingSize;
            servings = builder.servings;
            calories = builder.calories;
            fat = builder.fat;
            sodium = builder.sodium;
            carbohydrate = builder.carbohydrate;
        }
    }
    


    对于验证,您可以使用早期验证(在每个 <field> 方法)或惰性验证(在build()方法中)。格式是一种python键值初始化。