Acquaintance JavaBean

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/suoyue_py/article/details/98169672

1. What is JavaBean

JavaBean is a software component Java programming language in a reusable, essentially a special Java classes.
Coding JavaBean specification:

  • The default constructor must have a public constructor with no arguments, but the compiler automatically generates
  • Public setter method and a getter method, so that an external program to set and get JavaBean properties

2. Access JavaBean properties

The class attribute refers to the member variable of the class. But the JavaBean properties and member variables is not a concept, it is the emergence of ways defined, these methods must follow certain naming conventions.
For example: a JavaBean contained in a property of type String name, then at least to be included in a JavaBean getName () and the setName () a process in the method declaration is as follows:

public String getName(){...}
public void setName(String name){...}

Explanation:

  • SUMMARY vivo by " ... " indicates omitted
  • getName () method: called getter method or property accessor to get lowercase prefix, followed by the first letter capitalized property name
  • called setter methods or properties: setName () method to modify an attribute name must be set in lowercase prefix, followed by the first letter to be capitalized

Note:
If the property is only a getter method, then the property is read-only attribute; if only attribute setter methods, the property is write-only property; if getter and setter methods are compared with read-write property. Typically attributes are defined as read-write property.

Exception:
If the property type is boolean, you should use the naming is / set, rather than get / set.
For example: a boolean attribute married, the corresponding method attribute declared as follows:

public boolean isMarried(){...}
public void setMarried(boolean married){...}

Visible, setter method married naming attribute has not changed, but the getter method becomes isMarried () method. If written getMarried () it is also possible, but isMarried () more consistent naming conventions.

Guess you like

Origin blog.csdn.net/suoyue_py/article/details/98169672