Why use java in the get () and set () method

get () and set () method is to get and set the mean, get only reference, set only assignment

A: Security

These two methods can only provide one for others, it can also set restrictions on the method, so that you can easily access and modify the class variables

II: Maintainability

Three: can control variables

With get () and set () you can control variables. If the get () and set () method which only put an assignment, then, and public variables should be no different. But imagine, if your variable score represents a student's math, in theory, could only score is an integer from 0 to 100, if you take the score to public, do not get () and set (), the user it is likely to enter a number other than 1 to 100, if you take the score to privata, then setScore () to set the score, you can control the behavior of the

original link: https: //blog.csdn.net / fututadeyoushang / article / details / 80941537

Four: As a norm, so many frameworks use

 

Released three original articles · won praise 0 · Views 1426

Guess you like

Origin blog.csdn.net/qq_41841880/article/details/104523951