Originally intended to start June 1, an article written, although June 1 and I do not have any relationship, OK, continue to the second article ~ ~
Array
1. Reverse mode, although there are methods to achieve, I am still more serious, these to understand the existing methods still have some effect.
Reverse
Mode 1:
public static int reverse (int[] arr) {
for (int x=0; x<arr.length/2; x + +) {
int temp = arr[x];
ARR[X] = Arr[arr.length-1-x];
Arr[arr.length-1-x] = temp;
}
}
Mode 2:
public static int reverse (int[] arr) {
for (int start=0,end=arr.length-1; start<=end; start++,end--) {
int temp = Arr[start];
Arr[start] = Arr[end];
Arr[end] = temp;
}
}
Packaging
- Shadow of the implementation of details, providing public access to the way
- Role
A: Hide implementation details and provide public access
B: Improve the reusability of code
C: Improve the security of your code
3.private Keyword: A member that has been modified by private and can only be accessed in this class
Inherited
Extract the same members from multiple classes into this class, and then let the other classes create a relationship with the standalone class
(1) Benefits of inheritance:
A: Improved reusability of the code
B: Improved maintainability of the code
C: Let the class and the class have a relationship, it is the polymorphism of the prerequisite polymorphism
(2) Disadvantages of inheritance:
A: To enhance the coupling of the class. Such a change in a class will affect other classes related to that class.
Principle: Low coupling, high cohesion.
Coupling: Class-to-class relationships
The ability to accomplish something by oneself
B: Breaking the encapsulation
(3) Considerations of succession:
A: Subclasses cannot inherit private members of parent class
B: Subclasses cannot inherit the constructor of a parent class, but can access it through super
Polymorphic
The different states that the same object manifests at different times
(1) The precondition of polymorphism:
A: There is a succession or realization of the relationship.
B: There are methods to rewrite.
C: There is a parent or parent interface reference to the child class object.
(2) The benefits of polymorphism:
A: Improve the maintenance of code (inheritance embodies)
B: Improve code Extensibility (polymorphic representation)
(3) The disadvantages of polymorphism:
The parent cannot use the unique features of the child.
Construction method
A: Used to initialize the object's data
B: note
1. Method name and class name are the same
2. There is no return value type, and even void cannot have
3. There is no return value, but can have a return; (because the return can be written at the end of any void method)
C: In inheritance, the constructor of a subclass accesses the constructor of the parent class first (reason: In order to access the data of the parent class to the sub-class), of course, the subclass can also explicitly use Super to invoke the parent class of the argument constructs
code block
(1) Local code block: Used to define the period of the variable, early release, improve memory utilization.
(2) Constructing code blocks: the same code in multiple construction methods can be put here, before each construction method executes, the construction code block is executed first.
(3) Static code block: Initializes the data for the class and executes only once.
Execution order: Static code block > Construction Code block > Construction Method
Method overloads and method overrides:
Method overloading: In the same class, the same method name appears, with different parameter lists, regardless of the return value.
Method overrides: In subclasses, occurrences of the same method declaration as in the parent class
Java Fundamentals (ii)