Why java in the set / get method?

  Today in the brush Denver, I saw an article  Java Getter / Setter "anti-pit Guide" came  there and said:

By using Getter / Setter methods to access variables (get) and update (set) will become controllable. Consider the following codes Setter methods

public void setName(String name) {
    if (name == null || "".equals(name)) {
        throw new IllegalArgumentException();
    }
    this.name = name;
}

  I feel a bit wrong ah, because before the company is prohibited to add any logic in the set / get method, the entity class is a simple entity classes, without any logic processing, no.

  Since the argument of the article is not sufficient, then the java in the end is why the need to set / get methods? But also has become a common sense one kind javaer of the
  Internet there are several versions, some said to be encapsulated in order to reflect the characteristics of java language, some say that for safety, and some say that in order to meet specifications, say not in place.
  In fact, the use of set / get is to control variables
  if there is a property that is supplied to the external view, but not modify, so only need to provide get (), does not provide set () can be. If the object. The way property use, can not be done. Further, spring framework, generating entity class are all set / get, so it also forms a standardized

Published 158 original articles · won praise 193 · Views 1.48 million +

Guess you like

Origin blog.csdn.net/zy_281870667/article/details/102455205