1. Encapsulation)
Definition: encapsulation encapsulates information that the client cannot see. So that internal execution is not transparent to the outside, it is a black box, the client does not need internal resources to achieve his goal.
1. Hide the internal implementation details of things
2. provide consistent public interfaces-indirect access to hidden data
3. maintainability
Ii. Inheritance)
Java inheritance features
Inheritance: Can a parent class member inherit from a subclass?
Restrictions on access to class members and subclass inheritance:(Strict to wide
)
Private, which cannot be inherited internally
(Default) This class + the same package as the Steamed Stuffed Bun class can be inherited
Protected this class + same package + subclass can inherit
Public public can be accessed anywhere to inherit from subclass
Overwrite
1. Definition: overwrite a method and overwrite it to achieve different effects.
2. Usage:
A,The most familiar coverage is the implementation of interface methods.
B. Methods in the parent class may be overwritten in the subclass during inheritance.
3. Conditions for generating "Overwrite:
1. Method Name: Same
2. parameter table: Same (number, type)
3. Access limiters: Same or wider
4. Return Value Type: the same type or the type returned by the subclass is the type returned by the parent class.
5. You cannot throw more exceptions than subclass (parent class ).
Note: The static method created in the subclass does not overwrite the static method with the same name in the parent class.
Class Parent
{
Public void nonstaticmethod ()
{
System. Out. println ("Parent's non-static method is called ");
}
Public static void staticmethod ()
{
System. Out. println ("Parent's static method is called ");
}
}
Class Child extends parent
{
Public void nonstaticmethod ()
{
System. Out. println ("Child's non-static method is called ");
}
Public static void staticmethod ()
{
System. Out. println ("Child's static method is called ");
}
}
Public class test
{
Public static void main (string ARGs [])
{
Parent p1 = new parent ();
Parent P2 = new child ();
Child C = new child ();
System. Out. Print ("parent. Static:"); parent. staticmethod ();
System. Out. Print ("p1.static:"); p1.staticmethod ();
System. Out. Print ("p2.static:"); p2.staticmethod ();
System. Out. Print ("p1.nonstatic:"); p1.nonstaticmethod ();
System. Out. Print ("p2.nonstatic:"); p2.nonstaticmethod ();
System. Out. Print ("child. Static:"); Child. staticmethod ();
System. Out. Print ("C. Static:"); C. staticmethod ();
System. Out. Print ("C. nonstatic:"); C. nonstaticmethod ();
}
}
The program runs as follows:
Parent. Static: parent's static method is called
P1.static: parent's static method is called
P2.static: parent's static method is called
P1.nonstatic: parent's non-static method is called
P2.nonstatic: Child's non-static method is called
Child. Static: Child's static method is called
C. Static: Child's static method is called
C. nonstatic: Child's non-static method is called
It is worth noting that P2 is actually a reference of the Child type. However, when calling a static method, it executes the static method of the parent class instead of the static method of the child, when calling the non-static method of P2, the child non-static method is executed. Why? The reason is that the static method matches the static method with the reference type of the class during compilation, rather than matching the class reference at runtime. Therefore, we conclude that the static method created in the subclass does not overwrite the static method with the same name in the parent class.
Structure
(Java programming ideology is defined as: constructor initialization)
Object Construction Process:
First, allocate memory space for the object, including the space of visible or invisible variables of all its parent classes, and initialize these variables as default values, such as int Type 0 and boolean type false, the object type is null ....
Then, use the following five steps to initialize the new object:
1) Assign parameters to the specified constructor;
2) If the first statement of the specified constructor uses the this pointer to explicitly call other constructor methods of this class, the five steps are performed recursively; if the execution process is normal, skip to Step 5;
3) if the first statement of the constructor does not explicitly call other constructor methods of the class, and the class is not an object class (the object is the ancestor of all other classes ), call the explicit (using the super pointer) or implicitly specified parent class constructor to recursively execute these five steps. If the execution process is normal, skip to Step 5;
4) initialize the variables of this class according to the definition sequence of the variables in the class. If the execution process is normal, skip to Step 5;
5) execute the remaining statements in this constructor. If the execution process is normal, the process ends.
The most important thing for analyzing the examples in this article isThe constructor call of the parent class occurs before the subclass variable initialization.". We can use the following example to prove this:
Column 1
Class animal
{
Animal ()
{
System. Out. println ("animal ");
}
}
Class cat extends animal
{
CAT ()
{
System. Out. println ("cat ");
}
}
Class store
{
Store ()
{
System. Out. println ("Store ");
}
}
Public class PetStore extends store
{
Cat cat = new CAT ();
PetStore ()
{
System. Out. println ("PetStore ");
}
Public static void main (string [] ARGs)
{
New PetStore ();
}
}
Run this code and the execution result is as follows:
Store
Animal
Cat
PetStore
From the results, we can see that when creating a PetStore class instance, we first call its parent class store constructor; then we try to create and initialize the variable cat; when we create a cat, first, the cat class's parent class animal constructor is called; then the cat constructor is followed by the cat constructor, and finally the constructor of the PetStore class.
Column 2
Class animal
{
Animal ()
{
System. Out. println ("animal ");
}
}
Class cat extends animal
{
CAT ()
{
System. Out. println ("cat ");
}
}
Class cat2 extends animal
{
Cat2 ()
{
System. Out. println ("cat2 ");
}
}
Class store
{
Cat2 cat = new cat2 ();
Store ()
{
System. Out. println ("Store ");
}
}
Public class PetStore extends store
{
Cat cat = new CAT ();
PetStore ()
{
System. Out. println ("PetStore ");
}
Public static void main (string [] ARGs)
{
New PetStore ();
}
}
Run this code and the execution result is as follows:
Animal
Cat2
Store
Animal
Cat
PetStore
This example 1 and 2 give me a better understanding of the class construction process:
When the class is in the new instance
A. Step 1: Construct the member variables of the class
Note the following:
A) even if the variable definitions are scattered between the method definitions, they will still be initialized before any method (including the constructor) is called.
B) The initialization sequence is "static" objects (static objects are not initialized after initialization), and then "non-static" objects.
Step 2 call the constructor.
B. If there is a parent class, it is to first construct the member variables of the parent class, then call the construction method of the parent class, and then.
This usage
This in Java is everywhere and used in many ways. The following points are available:
1. This refers to the current object itself.
This reference should be added when the variables or functions of the object must be explicitly specified in a class. In the following example:
Public class hello
{
String S = "hello ";
Public Hello (string S)
{
System. Out. println ("s =" + S );
System. Out. println ("1-> This. S =" + this. s );
This. S = s;
System. Out. println ("2-> This. S =" + this. s );
}
Public static void main (string [] ARGs)
{
Hello x = new Hello ("helloworld! ");
}
}
Running result:
S = helloworld!
1-> This. S = Hello
2-> This. S = helloworld!
In this example, In the constructor hello, the parameter S has the same name as the variable s in the Hello class. In this case, if the parameter S is operated directly, the parameter S is operated.
To operate the member variable s of the Hello class, use this to reference it.
Class
{
Public ()
{
New B (this). Print ();
}
Public void print ()
{
System. Out. println ("hello from! ");
}
}
Class B
{
A;
Public B ()
{
This. A =;
}
Public void print ()
{
A. Print ();
System. Out. println ("hello from B! ");
}
}
Public Class C
{
Public static void main (string [] ARGs)
{
A x = new ();
}
}
Running result:
Hello from!
Hello from B!
In this example, the constructor of object a uses new B (this) to pass object A as a parameter to the constructor of object B.
2. In constructor, you can use this to call other constructor in the same class, as shown in
Class flower
{
Flower (INT pig)
{
System. Out. println ("pig:" + pig );
}
Flower (string SS)
{
Ss = "string ";
System. Out. println ("SS:" + SS );
}
Flower (INT pig, string SS)
{
This (pig );
System. Out. println ("pigpig" + pig );
}
}
Public class ZX
{
Public static void main (string [] ARGs)
{
Flower A = new flower (7, "Java ");
}
}
Running result:
Pig: 7;
Pigpig: 7;
It is worth noting that: 1: When constructing and calling another constructor, the call action must be placed at the starting position. 2: The constructor cannot be called in any function other than the constructor. Static
1. When calling a static method, no objects need to be generated. Because the static method belongs to the class itself, not the object. when calling the method, the class name. Static Method Name () can be used.
2.
Class Point
{
Int X;
Static int y; // defines a static variable.
Static void output ()
{
System. Out. println ("out ");
}
Public static void main (string [] ARGs)
{
Point. Output (); // call the static method
Point pt1 = new point ();
Point pt2 = new point ();
Pt1.y = 5;
Pt2.y = 6;
System. Out. println (pt1.y );
System. Out. println (pt2.y );
}
}
Result
Pt1 6
Pt2 6
Static constants and static methods belong to only one class, and memory space is available during loading. all objects share the same static variable, the same static method .. reference is directly referenced by the class name.
But non-static methods can call static methods and static variables. In turn, static methods cannot call non-static variables and non-static methods. (that is, X)
Final
The final class cannot be inherited. Therefore, the member methods of the final class have no chance to be overwritten. By default, they are all final. In Design Class, if this class does not need a subclass, the implementation details of the class cannot be changed, and is sure that this class will not be loaded and extended, it is designed as a final class.
1. Definition and programming skills: the final class cannot be inherited, and there is no subclass. The methods in the final class are final by default.
The final method cannot be overwritten by the quilt class, but can be inherited.
Final member variables indicate constants and can only be assigned once. The values do not change after the values are assigned.
Final cannot be used to modify the constructor.
Note: The Private member method of the parent class cannot be overwritten by the quilt class method. Therefore, the private type method is of the final type by default.
2. Final Method
If a class does not allow its subclass to overwrite a method, you can declare this method as a final method.
There are two reasons for using the final method:
First, lock the method to prevent any inheritance class from modifying its meaning and implementation.
Second, efficient. The compiler transfers the final method to the embedded mechanism to greatly improve the execution efficiency.
Tip: Final Static type member variables. Static means that only one copy of the variable exists. Final makes it unchangeable.
3. about final member assignment:. final members can and can only be initialized once. final members must be declared or initialized in the constructor, but not elsewhere.
Eg1
Public class AOX
{
Int height;
AOX (int h)
{
Height = h;
}
Void BB ()
{
System. Out. println (height );
}
Public static void main (string [] ARGs)
{
AOX boxobj = new AOX (25 );
Boxobj. BB ();
Boxobj = new AOX (32 );
Boxobj. BB ();
}
}
Running result:
25
32
Eg2
Public class AOX
{
Int height;
AOX (int h)
{
Height = h;
}
Void BB ()
{
System. Out. println (height );
}
Public static void main (string [] ARGs)
{
AOX boxobj = new AOX (25 );
Boxobj. BB ();
// Boxobj = new AOX (32); error!
Boxobj. BB ();
}
}
Super
1. Super (parameter): Call a constructor in the base class (it should be the first statement in the constructor)
2. super: it refers to the members in the direct parent class of the current object (used to access member data or functions in the hidden parent class in the direct parent class, when the base class and the derived class have the same member definition)
For example, super. variable name
Super. Member letter data name (real parameter)
Package
1. in essence, a package is a mechanism that combines classes to form code modules. It can be regarded as a "folder", while a variety of packages are "Files ".
Purpose: avoid naming conflicts. You can protect classes, data, and methods in a wider range.
2. Create a package
Package A; // A is the package name.
3. Import package
1) Import Package A. * // A is the package name. The character "*" imports all classes and interfaces in the package.
2) Import Package A. B // A indicates the package name and B indicates the class name. This operation imports Class B in Package A without importing other classes.
4. Use the classes of other packages without importing the package. The operation is as follows:
A. B x = new A. B (); // A indicates the package name, B indicates the class in the package, and X indicates the Instance name of the newly created class.