[Java Basics] setter and getter methods, javasettergetter
1 // the following code sets and obtains the Student name and score. 2 class lesson5homework 3 {4 public static void main (String [] args) 5 {6 TestCode TC = new TestCode (); 7 TC. studentTest (); // call test class 8} 9} 10 class Student 11 // set, obtain the student value and the Boolean rn and rs 12 {13 private String name; 14 private int score; 15 private boolean rn indicating whether the name and score values are correct; 16 private boolean rs; 17 // private value. Use the setter and getter methods to provide external access in the format of 18/* 19: 20 getter (for obtaining ): 21 [non-private modifier] field type get field name (large first letter) Write) () 22 {23 return field name; 24} 25 setter (used for setting): 26 [non-private modifier] void set field name (uppercase) (Field Type Variable) 27 {28 field = variable; 29} 30 31 except: the setter and is methods are Boolean types. The 32 setter format is the same as the above. The is method only needs to program set as is. The following is an example. 33 */34 35 public void setName (String n) // setter and getter methods of name 36 {37 if (n! = "") 38 {39 name = n; 40 setRn (true); 41} 42 else 43 {44 System. out. println ("ERROR! Name is error! "); 45 setRn (false); 46} 47} 48 public String getName () 49 {50 return name; 51} 52 53 public void setScore (int s) // score setter and getter Methods 54 {55 if (s> = 0 & s <= 100) 56 {57 score = s; 58 setRs (true ); 59} 60 else 61 {62 System. out. println ("ERROR! Score is error! "); 63 setRs (false); 64} 65} 66 public int getScore () 67 {68 return score; 69} 70 71 public void setRn (boolean xn) // Boolean setter method and is method 72 {73 rn = xn; 74} 75 public boolean isRn () 76 {77 return rn; 78} 79 80 public void setRs (boolean xs) 81 {82 rs = xs; 83} 84 public boolean isRs () 85 {86 return rs; 87} 88} 89 class TestCode 90 {91 public void StudentTest () // Test 92 {93 System. out. println ("test code of student:"); 94 printStudent ("jack", 100); 95 printStudent ("jack", 101); 96 printStudent ("jack ", -1); 97 printStudent ("", 100); 98} 99 private void printStudent (String name, int score) 100 {101 Student s = new Student (); 102 s. setName (name); 103 s. setScore (score); // set the value 104 if (s. isRn () & (s. isRs () // if correct, output 105 System. out. println ("Name:" + s. getName () + ", Score:" + s. getScore (); 106 107} 108}
I am a newbie, and the above is a summary of my online self-learning experience. If you have any mistakes, please note. There are a lot of new kids shoes to talk about, and great gods give more advice. I wish you a happy life.
What is the setter // getter () method in JAVA?
Use the code to explain:
Public class Student {
String name;
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
Student s1 = new Student ();
S1.setName ("James ");
System. out. println ("Student name:" + s1.getName ());
}
}
The above indicates creating a student class, declaring a name variable, and giving it the getter and setter methods.
Let's first talk about the setter method. No value is assigned when the name variable is created at the beginning, and then we use this method to assign it "James"
Let's talk about the getter method. After the name variable has the value "James", you can use this method to call it out.
What are the roles of java getter and setter?
In fact, it is clear that this is only two methods of java class (especially a javabean) to provide two portals for setting and accessing a variable (field, in many cases, as you said, it is no different to define public. The specific reasons are as follows:
(1) Since many frameworks call these two methods to achieve communication, this is a habit.
(2) The get/set method does not necessarily simply assign values or return values. Here we can control some permissions, for example, not every role can assign values. For example, if the returned value is not necessarily the value itself, it can be processed, such as encryption, which cannot be defined using public.
Do you think public is not good? You can just remove it. Implement hide? You can use get/set to get it and set it. How is this called hiding?