1.The SET method & GET method
We can use SET method to set value for a private member in a class.
Here it shows the advantages of SET method
- It's a indirect access,but it can make a judgement for the passed values
public class Person {
private String name;
// The set method
public void setName(String name){
//for security,we can add some conditions to constraint its range of application
if(name.length() == 8) {
//this.name means it indicates the value that users pass through setNmae(String name)
this.name = name;
}
}
}
The GET method
public String getName(){
return name;
//There is no need to use this.name for it's unambiguous
}