------- Android training, Java training, and hope to communicate with you! ----------
After seeing the video from instructor Zhang, he said: whoever owns the data, anyone will provide the data operation method. This is a very important experience of object orientation. With the accumulation of time and accumulation of knowledge, I gradually realized the meaning of this sentence. So, I reviewed the object-oriented knowledge. It can be said that, looking back, the object is in the dark.
There are still a lot of knowledge points related to object-oriented, so I will briefly list the knowledge points, mainly to understand the idea of object-oriented.
Object-oriented features
Object-oriented and process-oriented are both ideas. process-oriented: focuses on functional behaviors. Object-oriented: encapsulates functions into objects and emphasizes functional objects.
Note: Object-oriented is based on process-oriented.
Three main characteristics of object-oriented architecture: encapsulation (encapsulation) Inheritance (inheritance) polymorphism (polymorphism)
Concept
Class: The description of a thing in Java is embodied in the form of a class. A class is an abstract and conceptual definition of a specific thing.
Object: an object is an entity that exists in such a thing.
Define the members of a class (member variables and member functions ).
Member variables:
1. member variables are defined in the class and can be accessed throughout the class.
2. member variables are created with the object and exist in the heap memory where the object is located.
3. member variables have default initialization values.
Local variables:
1. Local variables are defined only within the local range, such as within the function or in the statement.
2. Local variables exist in the stack memory.
3. The variable space is automatically released when the range is reached.
4. No Default initialization value is available for local variables.
Encapsulation
It is used to hide the attributes and implementation details of an object and only provide public access.
Benefits:
1. Isolate changes.
2. Easy to use.
3. improve reusability.
4. improve security.
Encapsulation principles:
1. Hide the content that does not need to be provided externally.
2. Hide all attributes and provide public methods to access them.
Private keywords: A permission modifier used to modify members (member variables and member functions). Private Members are only valid in this class.
Constructor
Features:
1. The function name is the same as the class name.
2. do not define the return value type
3. The return statement cannot be written.
Purpose: Initialize the object.
Note: 1. default constructor features. 2. Multiple constructors exist in the form of overloading.
This keyword
Feature: This indicates the reference of the object to which the function belongs.
In other words, this represents the reference of this class object.
Static keywords
It is used to modify members (member variables and member functions). The modified members have the following features:
1. Loading with classes
2. takes precedence over object existence
3. shared by all objects
4. can be called directly by Class Name
Note:
1. Can static methods only access static members?
2. This and super keywords cannot be written in static methods.
3. The main function is static.
Static advantages and disadvantages:
Benefits: the shared data of objects is stored in separate space to save space. There is no need to store a copy of each object. Can be called by class name.
Disadvantages: the lifecycle is too long, and access restrictions occur (static is good, but only static access is allowed !)
Construct a code block:
Purpose: Initialize the class. The object runs as soon as it is created and takes precedence over the constructor running.
Difference from constructor:
The constructor code block initializes all objects in a unified manner, and the constructor initializes the corresponding objects. The constructor code block defines the common initialization content of different objects.
Static code block:
Format: static
{
Static code block content
}
Features: it is executed only once as the class is loaded and takes precedence over the main function for class initialization!
Singleton design mode:
Solve the problem that a class can only have one object in the memory.
To ensure the uniqueness of an object:
1. To prevent other programs from creating too many objects of this class, prohibit other programs from creating this object first.
(Privatize constructors)
2. To allow other programs to access the objects of this class, we have to customize an object in this class.
(Create a class object in the class)
3. To make it easier for other programs to access custom objects, some access methods can be provided externally.
(Provide a method to obtain the object)
Hungry Chinese Style: the class has already created an object as soon as it enters the memory.
Class Single {
Private Static Single S = new single ();
Private single (){};
Public static single getinstance (){
Return S;
}
}
Lazy: the object is initialized only when the method is called. It is also called the delayed loading of the object.
Class Single {
Private Static Single S = NULL;
Private single (){};
Public static single getinstance (){
If (S = NULL ){
S = new single ();
Return S;
}}
Inheritance
Concept of Inheritance
1. When multiple classes have the same attributes and behaviors, extract the content to a separate class. Therefore, multiple classes do not need to define these attributes and behaviors, you only need to inherit the separate class.
2. Multiple classes can be called subclasses. This class is called a parent class or a superclass.
3. Subclass can directly access non-private attributes and behaviors in the parent class.
4. Use the extends keyword to generate an inheritance relationship between the class and the class.
Note: 1. Inheritance improves code reuse. The emergence of inheritance creates a relationship between classes and provides the premise of polymorphism.
2. Java only supports single inheritance, does not support multiple inheritance, and JAVA supports multi-layer inheritance (Inheritance System)
Super Keyword:
1. The usage of super and this is the same. This indicates the class application, and super indicates the parent class reference.
2. When a sub-parent class has a member of the same name, you can use super to differentiate it.
3. When the subclass calls the parent class constructor, the super statement can be used.
Function coverage:
1. When a method of the same type as the parent class appears in the subclass, The overwrite operation, also known as "re-write" or "re-write", will appear.
2. Private methods in the parent class cannot be overwritten.
3. In the subclass override method, you can continue to use the overwritten method through the super. function name.
Considerations:
1. When overwriting, The subclass method permission must be greater than or equal to the parent class method permission.
2. static data can only cover static data.
Final keywords:
1. Final can modify classes, methods, and variables.
2. The final modified class cannot be inherited.
3. The final modification method cannot be overwritten.
4. The final variable is a constant. It can be assigned only once.
5. The internal class can only access local variables modified by final.
Abstract class:
Java can define a method without a method body. The specific implementation of this method is completed by sub-classes. This method is called an abstract method. classes that contain abstract methods are abstract classes.
Abstract class features:
(1) abstract classes and abstract methods must be modified with abstract keywords.
(2) An abstract method is defined in an abstract class and has only a method declaration and no method body.
Format: modifier abstract Return Value Type Function Name (parameter list );
(3) abstract classes cannot be instantiated, that is, they cannot be used to create objects with new. The reason is as follows:
1. abstract classes are extracted from specific objects. They are not specific and have no corresponding instances. For example, a dog is an abstract concept, which actually exists as a wolf or a dog.
2. Even if the abstract class creates an object, calling the abstract method is meaningless.
(4) An abstract class is instantiated by its subclass. The subclass must overwrite all abstract methods in the abstract class before creating an object. Otherwise, the subclass is also an abstract class.
Interface:
Format:
Interface {}
The member modifiers in the interface are fixed.
Member constant: public static final
Member functions: public abstract
Interface features:
1. interfaces are externally exposed rules.
2. The interface is the Function Extension of the program.
3. The interface can be used for multiple implementations.
4. There is an implementation relationship between the class and the interface, and the class can inherit a class and implement multiple interfaces at the same time.
5. interfaces and interfaces can have an inheritance relationship.
Polymorphism:
Definition: multiple forms of existence of a certain type of things.
Embodiment: the reference of the parent class or interface points to or receives its own subclass object.
Role: the existence of polymorphism improves program scalability and maintainability in the future.
Prerequisites: 1. There must be inheritance or implementation relationships; 2. There must be overwriting operations.
Polymorphism features:
Member functions:
1. During Compilation: Check whether the referenced variable belongs to any called member in the class.
2. At runtime: Check whether the class to which the object belongs has called members. Member variable: only the class to which the referenced variable belongs.
Internal class:
Define a class in another class. The class inside is called an internal class (built-in class, nested class ).
Access features:
1. The internal class can directly access members in the external class, including private members.
2. To access members of an external class, you must create an object for the internal class.
Anonymous internal class
Premise: an internal class can inherit or implement an external class or interface.
Format:
New External class name or Interface Name () {overwrite the code in the class or interface (you can also customize the content.
)}
Easy to understand: it is to create an anonymous object of a subclass of an external class or interface with content.
Summary:
I feel that I still need to understand the idea about the object-oriented knowledge at this stage. It does not seem very useful to rely solely on rote memorization. Special attention should be paid to the polymorphism among the three features of object-oriented, which is widely used in subsequent Java learning. In addition, internal classes are also the focus of learning.