Outline
Object-oriented programming language is a simulation of the objective world, the objective world, member variables are hidden inside the object, the outside world can not directly manipulate and modify. Package may be considered as a protective barrier against the class codes and other types of data are accessed randomly. To access this kind of data, you must specify the way through. Appropriate encapsulation makes the code easier to understand and maintain, and enhance the security of the code.
Principles package
The property is hidden, if the need to access a property, providing public access to its methods.
Step package
1, using the private key to modify the member variables.
2, member variables need to access, provide a common access method, which is defined corresponding getXxx method, setXxx method.
Package key operation --private
private meanings
1, Private is a privilege modifier, on behalf of least privilege.
2, can be modified member variables and member methods to achieve the purpose of the hidden attribute
3, the member variables and member methods after private modification, only be accessed in this category
private use format
private data type variable name;
Why use private modification?
Because not to expose properties to others
this keyword
this meaning
this refers to the current object where the class representative (address value) that their object references.
Remember : method is called which object, this method represents that object. That is who to call, who this represents.
Construction method
When an object is created when the method is used to initialize the object constructor, the object member variable to the initial value assigned.
Role: initialize the object, the object is assigned to the member variables initial
Definition Format constructor
Specific examples of the package
public class the Person { // define the member variables Private String name; // Name Private int Age; // Age Private char gendar; // Gender // define the members of method // There arg constructor public the Person () { } // No arg constructor public the Person (String name, int Age, char gendar) { the this .name = name; the this .age = Age; the this .gendar = gendar; } //get和set方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public char getGendar() { return gendar; } public void setGendar(char gendar) { the this .gendar = gendar; } / ** * Study Method * * / public void Study () { System.out.println ( "good school study, every day" ); } / ** * SLEEP (); sleep * / public void sLEEP () { System.out.println ( "study day, can sleep" ); } }