Java basics-Object-oriented (encapsulation overview)
1. Basic overview and use of object-oriented encapsulation
(1). What is the idea of encapsulation
Define a basic student class:
Member Variable: name,age
Member Method: Show () method
Test class:
Studentdemo
Create a Student object and use the method inside
In the process of defining this case, we found a problem:
Through the object to assign values to the member variable, you can assign some illegal data, such as age we define the type of int, that is, we assign a value of 200 is also reasonable, but in practice, this one is unreasonable, we should need to do a limit, should be
In this way, we should make a judgment on the data before we make the data assignment. However, where this judgment should be reasonable, when called, the Studentdemo class is already a test class, here is generally the creation of objects, and call methods
So we should define the checksum of this data in the student class. So where in the student class to do this data validation, because the data validation to rely on a number of logical statements. Logical statements should be defined in the method, so we should
A method is provided in the student class to validate the data.
We give a method to verify the data, but when it is used, it happens not to call the method to assign value, or directly assign value, that is, we set the calibration method does not play a role, we have to force the request to use our method
Instead of calling the member variable directly to assign a value. So how do you force a request not to use member variables directly?
In the case of a situation, we can use the keyword provided by Java to private, using the private modifier member variable, so that member variables can only be accessed in this class, and provide the corresponding getxxx () method and the Setxx () method of the member variable.
To sum up, what we are actually talking about is a package of process and a concept of encapsulation.
(2). Package
Encapsulation refers to the properties and implementation details of hidden objects, providing public access only to the outside. This means privatizing the properties of member variables (using private adornments), and then providing public access (that is, the GetXXX () method of the response and the Setxx () method)
(3). Basic code implementation for encapsulation operations
public class Student {
Name
private String name;
Age
private int age;
/**
* @return The name
*/
Public String GetName () {
return name;
}
/**
* @param name the name to set
*/
public void SetName (String name) {
THIS.name = name;
}
/**
* @return The Age
*/
public int getage () {
return age;
}
/**
* @param age-The age-to set
*/
public void Setage (int.) {
if (age>120| | age<0) {
System.out.println ("Age problem");
}else{
This.age = age;
}
}
}
(4). Packaging Benefits and Design principles
A. Benefits:
Hide implementation details and provide only public access to the outside
Improved reusability of the code
Improve security
B: Principles
To hide what you don't need to provide to the outside world.
Hides the attribute (which is also the member variable) and provides a public access method.
2.this keywords
This: Represents a reference to an object of the current class. Simple, it is an object representing the current class.
Note: Who calls this method, in the inside of the method, this represents who.
The application scenario for this:
Resolve local variable hidden member variables (the use of variables follows the nearest principle as they are used)
Examples of Use:
public class Student {
Name
private String name;
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
}
Detailed Code usage
SetName () In this method, there are two name, one is the name of the member method, and the other is the name inside the form parameter, then how to distinguish and pass the parameter when using?
Since the use of parameters there is a near principle, that is, who is close to me, I will assign the value to it, the use of the parameters of the transfer is this:
Such as:
Student s =new Student ();
S.setname ("Xiao Ming");
During the pass of the parameter, the variable value xiaoming is first passed to name in SetName (String name), after which the name is the right name in This.name=name, because the This keyword represents the use of the current class object, i.e.
This.name=student.name, that is, pass to the member variable: the name of the private String name, so that we can call the class outside the time we want to assign the parameters we need to the student class object in the member variable.
3. Program code parsing: Student s=new Student (); Doing something in memory
(1). Load the Student.class inside the method area into memory
(2). Open a space for the variable s in the stack memory
(3). Request a space for a student object in heap memory
(4). Default initialization of member variables--null,0
(5). Display initialization of member variables--specific parameters assigned (Xiaoming-18)
(6). By constructing a method, the member variable is initialized, which is the new assignment (null,0---> xiaoming, 18)
(7). This time if you want to access the memory of the value that exists, that is: Xiaoming, 18
(8). The address of the stored variable in the heap memory is then assigned to student s in the stack, and the initialization data is complete.
4.static keywords
(1). Basic overview
Our definition of a student class, because the name and age are changing, so we need to define a variable to accept, but we are in use when all students of the international are Chinese, using the same nationality, we use the time every time we have to
Create a new nationality, give the same value, open up space in the heap memory, and waste resources.
When there are common member variables for multiple objects, that is, when the same variable can be used together, Java provides the keyword static
(2). Characteristics of the Static keyword
A: Loaded with the class (the main method is also the static method)
B: Precedence over object existence
C: Shared by all objects of the class
such as: A class of public one edit number
In fact: This is a feature that tells us when to use static
If a member variable is shared by all objects, it should be defined as static.
Practical Examples:
Water dispenser (is a class common one water dispenser, should use static decoration)
Water cups (each person needs to have a water cup, so should not be defined as static modification)
D: Can be called by the class name
In fact, it can be called by the object name itself, because it is an internal variable belonging to a class, and it is still public.
(3). Considerations for the Static keyword
A: In a static method there is no keyword this
How to understand it?
Static is loaded as the class loads, and this is present as the object is created.
Static than object exists first. So what comes in front of you is not able to access the stuff that comes in behind.
B: Static methods only have access to static member variables and static member methods
Static methods
Member variables: Only static variables can be accessed
Member method: Only access to member methods
Non-static methods:
Member variable: can be static or non-static
Member methods: can be static or non-static member methods
Simple to remember:
Static only can access static.
(4). The difference between a static variable and a general member variable
A: Belong to different
Static variables: Static variables belong to classes, so they are also referred to as class variables
Member variable: a member variable belongs to an object, so it is also referred to as an instance variable (object variable)
B: The location in memory is different
Static variables stored in the static area of the method area
Member variables are stored in heap memory
C: The time in memory appears different
Static variables are loaded as the class loads, disappearing as the exhaustion disappears
Member variables are created as objects are created and disappear as objects disappear
D: Call Different
A static variable can be called by a class name, or it can be called by an object
Member variables can only be called through objects.
5.main Method Explanation
Puclic static void Main (string[] args) {...}
Detailed
Public: Access is the most common, because the main () method is called by the JVM, so the permissions are large enough.
Static: It is not necessary to create an object, it can be called by the class name, and is convenient for the JVM.
void: As we have said, the return value of the method is returned to the caller, and the main method is called by the JVM and is meaningless to return to the JVM.
Main: Is a common method of entry. Most languages use the main () method as the entrance.
String[] args: This is an array of strings. Where did the value go? How exactly does this thing work? How to give a value?
At the very beginning, this thing was used for keyboard entry, and its data format was:
Java Maindemo Hello World Java
Java Basics---Object-oriented (2)