set () and get () method

In many programs, like the definition of a variable privata, then add get (), set () method for this private variable. So why does not directly define a public variable it? What are the benefits of doing so in the end there and the significance of it? Is it really just to code specifications? Give me a break, whether you believe it or not, anyway, I do not believe! With this question the answer I was looking for on the Internet, is really different opinions, ah, everyone can speak a little of this, but they are nothing but enough to convince me! (.. So here doing some conclusions, and to share our common learning and common progress if you have any thoughts and ideas, but also welcome to put forward, we communicate together) to see where there is a saying:

  "GET () and set () method to play the role of the outside world closed, for example: If your trouser pocket a lot of money, you would not have to dig for others, only he can certainly get, or you show it to him, set and get quite and with your hands your class is quite variable with your pocket money for outside people who you do not know what class he knew he would get a call this something "

  Wow! Seemingly answered very professional, very image, very lively, very reasonable, ah, I'm going to almost worship, but in the nick of time, I calmed down. TNN's, it was almost fooled you gave this example did not answer substantive questions Well, will illustrate the child who can not afford injuries ah ~~~~ (> _ <) ~~~~. If we want to get () and set () likened hand, it can not be likened to your own hands, because the get () and set () method is public property, anyone can call, which means that anyone hands can still get your money, and that variable is set to direct the public is there any difference? As for the outside people who know what class you have, this is only a matter variable naming convention, and half dime property does not exist, right? Want people to see and change the variables set to public, do not want people to see and change it into an privtata, very perfect, why get (), set () methods?

  I was confused, feeling reluctant to part, I left the great God, continue to look for answers. In fact, everyone knows that get () and set () method is certainly good, but what good is it but that is not clear, can only obediently did so (but no candy ha ha!), And the tool can help you automatically generated get () and set () method, is not trouble. But can not because they do not bother to do silly, not to consider why he does it, then we descend to the tool! ⊙ ﹏ ⊙ b Khan! Another tricky question, and continue to study. . .

  In fact, most people say that this is related to security issues, looks like a little sense, but it seems no basis, I ask what is unsafe to use public variables? Use get (), where the set () safe?
  Some people say that encapsulation, but the variable itself is encapsulated in a class inside ah!

  In fact, most people are just repeating what to say security, encapsulation, as long as people learn java will say, no one java book does not mention these terms, in fact, not many people really understand.

  I only know a get () and set () has the advantage, when the input .get (.set) will be able to show the property. To sum up all the settings are for external use are convenience only. If you develop a class for others to use, as long as people can know all the input .get properties. (Oh my God! The only advantage of such a Diudiu? I'm embarrassed to tell people that!)  

  Do not get () and set () is really just a norm only, fully equivalent to the public variables?

  Do not! People like me are absolutely not willing to put the question to find the answer comes down to the code specifications!

  I think the only thing you can convince me that the answer to temporarily: The get () and set () 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.

   Haha actually put this issue a few words can get, but for his essay vivid little, a little more nonsense, please forgive me! If you know get () and set () What other benefits, then advise younger look, very grateful!


---- Thank you for pointing cattle, the finishing from you for the reply and comments, continuously updated ------
using the get () and set () without the use of public member variables, because we have the design category to follow this principle:
to provide the kind of person to try to hide what class, just tell the user what you can do
using a class of people do not care what your class has only care what class provides functionality
so design class time to try not to provide public member variables, preferably a not only provides an interface
that principle is actually not just a matter of coding standards, into the work, which is collaboration between multiple project team or department guidelines
this principle is definitely not a wolf alone encoded program was thought out a racking our brains, which is experienced through the work of organized development program designed to ape out.
-------------------------------------------------- ----------
this problem is very simple, such as
setAge (int Age) {
IF (Age <1 || Age> 120)
  the throw new new Exception ( "the age you entered is not legal");
the else {}
  this.age = Age;
}

This is a public variable can not be done, security is one of the reasons, in addition to the landlord if you learned Hiberante and Spring, they would know
the variables of some kind, even the public, but also must be written on the set method, otherwise an error frame design when so required

so people are doing, you will do good without the bad, this is the standard

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Use the premise: When you want to access is private property encapsulated
reason for providing access methods: the reason to use private package, but also provide external access mode (set get), because you can add logic judgment statement in the access mode, access to the manipulate data, improve the robustness of the code.

class Student {
     private  int age;
     public  int getAge () { // Get the private authority under age // 4. 
        return age;
    }
    public  void the setAge ( int A) { // set name, the main function argument passed // 3. 
        IF (A> 0 && A < 200 is ) { // Here meet the real age is determined to make 
            age = A;
        }else{
            . System OUT .println ( " input error " );
        }
}
public class Test{
    public static void main(String[] args) {
        Student Student = new new Student (); // 1. 
        student.setAge ( 18 is ); // 2. 
        the System. OUT .println ( " Age: " + student.getAge ()); // will be passed arguments print // 5 
    }
}

 

 

Summary: The main role is to control

     Improve security code

     Encapsulation embodies

Guess you like

Origin www.cnblogs.com/WLCYSYS/p/12204647.html