Package (one of the object-oriented features) : refers to the properties and implementation details of hidden objects and provides public access only externally.
Benefits: Isolation of changes, ease of use, improved reusability, and security.
Encapsulation principle: to hide the content that does not need to be provided externally, to hide the attributes, and to provide public methods to access them.
This : represents an object. is the reference to the object to which the function belongs.
What does this mean? which object invokes the function of this, which represents which object, which is the reference to which object.
When do you use this when developing?
When defining a feature, this is used to represent the object if it is used internally to the object that invokes the feature.
This can also be used to construct calls between functions.
Call format: This (actual parameter);
The This object follows . The member properties and member methods are called (the General method);
The This object is followed by the () constructor that calls the corresponding parameter in this class.
Note: The constructor is called with this and must be defined in the first row of the constructor. Because constructors are used for initialization, the initialization action must be performed. Otherwise, the compilation fails.
Static : ★ keyword, which is a modifier used to decorate members (member variables and member functions).
Characteristics:
1, object sharing that wants to implement the commonality data in the object. This data can be modified statically.
2, a statically decorated member can be called directly by the class name. in other words, static members are more of a calling method. The class name. static mode .
3, static loads as the class loads. and takes precedence over the existence of the object.
Disadvantages:
1, some data are object-specific data, can not be statically modified. Because of that, the unique data becomes the shared data of the object. There is a problem with the description of the thing. Therefore, when defining static, it is important to be clear whether the data is shared by the object.
2, a static method can access only static members and cannot access non-static members.
Because the static method loads, it takes precedence over the existence of the object, so there is no way to access the members of the object.
3, You cannot use the This,super keyword in a static method.
This cannot be used because this represents an object, while static is possible without an object.
4, the main function is static.
when do you define static members? or, when defining a member, does it need to be statically decorated?
There are two types of members:
1 , the member variable. ( data sharing is static )
Whether the data for this member variable is the same for all objects:
If so, then the variable needs to be statically decorated because it is a shared data.
If not, then say that this is the object's unique data to be stored in the object.
2 , the member function. ( defined as static when no unique data is called in the method )
What if the member function needs to be statically decorated?
For reference, the function has access to the unique data within the object:
If there is access to unique data, the method cannot be statically decorated.
If no unique data has been accessed, then this method needs to be statically modified.
The difference between a member variable and a static variable:
1, the member variable belongs to the object. So also known as instance variables.
Static variables belong to the class. So it is also called a class variable.
2, the member variable exists in heap memory.
Static variables exist in the method area.
3, member variables exist with the creation of the object. Disappears as the object is recycled.
Static variables exist as classes are loaded. Disappears as the class disappears.
4, member variables can only be called by the object.
A static variable can be called by an object or by a class name.
Therefore, member variables can be called unique data for objects, and static variables are called shared data for objects.
Static Note: The static life cycle is very long.
Static code block: a code block area with a static keyword. defined in the class.
Function: The initialization of the class can be completed. A static block of code executes as the class is loaded and executes only once (new objects are executed only once). If the main function is in the same class, it takes precedence over the main function execution.
Public : Maximum access rights.
Static : No object required, direct class name.
void : The main function has no return value.
Main : The name of the main function-specific.
(string[] args) : The parameter of the main function, which is a parameter of the string array type, and the actual argument passed when the JVM calls the main method is new String[0].
By default, the JVM passes a string array of length 0, and when we run the class, we can also specify specific parameters for delivery. You can add parameters later in the console, when you run the class. The parameters are separated by a space. The JVM automatically stores these string parameters as elements in the args array.
the order in which static blocks of code, construction blocks, and constructors exist at the same time: Static code block à Building Code Blocks à constructor function;
generate Java Help document: command format: Javadoc–d folder name –auther–version *.java
/**//Format
* Class Description
* @author Author name
* @version Version number
*/
/**
* Method Description
* @param parameter description
* @return Return value description
*/
---------------------------------------------------------------------------------------------
Design Patterns : The most effective ideas for solving problems. is a set of repeated use, most people know, through the classification purposes, code design experience Summary. Design patterns are used in order to reuse code, make code easier for others to understand, and ensure code reliability.
There are 23 design patterns in Java:
Single case design mode: ★★★★★
Problem solved: Ensure that the object uniqueness of a class in memory.
For example: When multiple programs read a configuration file, it is recommended that the configuration file be encapsulated as an object. It is convenient to manipulate the data, and to ensure that multiple programs read the same profile object, it is necessary that the configuration file object is unique in memory.
Runtime () The method is designed by a single case design pattern.
How to ensure the uniqueness of the object?
Thought:
1, do not allow other programs to create the class object.
2, create an object of this class in this class.
3, provide methods for external, let other programs get this object.
Steps:
1 , because the constructor initialization is required for the creation of the object, as long as the constructors in this class are privatized, other programs cannot create the class object again;
2 , you create an object of this class in the class;
3 , define a method that returns the object so that other programs can get the object by means of the method. (Effect: controllable)
The code reflects:
1 , privatization of the constructor;
2 , creating a private and static object of this class;
3 , defines a public and static method that returns the object.
---------------------------------------------
A Hungry man type
Class single{
Private single () {}// privatization constructor.
private static Single S = new single (); creates a private and static object of this class.
public static single getinstance () {// defines a publicly-owned and static method that returns the object.
return s;
}
}
---------------------------------------------
Lazy type: Lazy loading mode.
Class single2{
Private Single2 () {}
private static Single2 s = null;
public static Single2 getinstance () {
if (s==null)
s = new Single2 ();
return s;
}
}
Java Learning (iii) object-oriented encapsulation