Confused about naming of JavaBean properties, with respect to getters and setters

前端 未结 3 1931
自闭症患者
自闭症患者 2020-12-19 11:29

I am making an application that uses Drools planner.

The @ValueRangeFromSolutionProperty is supposed to refer to a property from another class (NQ

相关标签:
3条回答
  • 2020-12-19 11:42

    The JavaBeans Specification says that for a property propertyName there should be a getter method getPropertyName() and/or a setter method setPropertyName().

    A property is defined by the only presence of the getter and setter methods and can also be a computed value. A instance variable on the object is not required.

    The specification defines the capitalization rules for properties and getter/setter methods:

    Thus when we extract a property or event name from the middle of an existing Java name, we normally convert the first character to lower case. However to support the occasional use of all upper-case names, we check if the first two characters of the name are both upper case and if so leave it alone. So for example,

    “FooBah” becomes “fooBah”, “Z” becomes “z”, “URL” becomes “URL


    The method is in fact implemented as:

    /*
    Utility method to take a string and convert it to normal Java variable name 
    capitalization. This normally means converting the first character from upper case to  
    lower case, but in the (unusual) special case when there is more than one character  
    and both the first and second characters are upper case, we leave it alone.
    
    Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays as "URL".
    
     Parameters:
         name The string to be decapitalized.
     Returns:
     The decapitalized version of the string.
     */
     public static String  decapitalize(String name) {
     if (name == null || name.length() == 0) {
     return name;
     }
     if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) &&
                         Character.isUpperCase(name.charAt(0))){
            return name;
         }
    
         char chars[] = name.toCharArray();
         chars[0] = Character.toLowerCase(chars[0]);
         return new String(chars);
     }
    

    So:

    1. if the name is null, return it as such
    2. if the name has first two characters in caps, return it as such
    3. all other strings, decapitalize first character
    0 讨论(0)
  • 2020-12-19 11:42

    That's defined by the JavaBeans naming conventions. The getter name will have "get" followed by the property name with the first letter capitalized.

    A related question with more information

    0 讨论(0)
  • 2020-12-19 11:47

    When javabeans refer to a "property" it is something with a get() and a set()-method. It doesn't care what the internal storage of data is (if there even is one).

    Thus a property "foo" has access methods getFoo() and setFoo(), what these methods do is irrelevant to the definition of the property.

    http://docs.oracle.com/javase/tutorial/javabeans/writing/properties.html

    0 讨论(0)
提交回复
热议问题