this keyword
this is an "implicit parameter" of a normal method, which is passed into the method by the system
Usage of this:
In a normal method, this always points to the object that calls the method
In the constructor, this always points to the object being initialized
this() calls the overloaded construction method to avoid the same initialization code; But it can only be used in the constructor and must be in the first sentence of the constructor
this cannot be used in static methods
package YuXiDemo; //Test the usage of this public class ThisTest { int a,b,c; ThisTest(){ System.out.println("Initialize object:" + this); } ThisTest(int a,int b){ //ThisTest(); In this way, the constructor cannot be called this();//Calling a parameterless constructor must be on the first line a = a;//Both a refer to local variables rather than member variables //With this, you can distinguish between member variables and local variables this.a = a; this.b = b; } ThisTest(int a,int b,int c){ this(a,b);//Calling a parameterized constructor must also be on the first line this.c = c; } void eat(){ System.out.println("Go home for dinner"); } void play(){ System.out.println("Current object:" + this); this.eat(); System.out.println("Let's play basketball together"); } public static void main(String[] args) { ThisTest tt = new ThisTest(); tt.play(); } }
static keyword
Static declared attributes or methods: static variables (class variables), static methods (class methods)
Static variables / static methods have the same life cycle as classes and are valid throughout program execution
It has the following characteristics:
It is a public variable of this class. It belongs to a class and is shared by all instances of this class. It is initialized when the class is loaded
There is only one static member variable
It is generally called with "class name. Class variable / method"
Non static members cannot be accessed directly in static methods
package YuXiDemo; public class TestStatic { int id;//account number String name;//Account name static String school = "Stay at home and go to college";//Name, which is a static property public TestStatic(int id,String name){ this.id = id; this.name = name; } void come(){ System.out.println(name); } public static void printSchool(){ //come(); If you call a non static member, an error will be reported during compilation; Non static members cannot be used in static methods System.out.println(school); } public static void main(String[] args) { TestStatic ts = new TestStatic(10086,"Little dragon"); TestStatic.printSchool(); TestStatic.school = "Wudaokou Vocational College"; TestStatic.printSchool(); } }
Code block
Concept: a piece of code enclosed by "{}"
Classification: it can be classified according to location
Normal code block: a code block defined directly in a method or statement
Construction code block: a code block written directly in a class
Static code block: a code block declared using static
Synchronous code blocks: learn when multithreading
Static initialization block
The construction method is used to initialize the common attributes of the object;
Static initialization block, used for class initialization operation and initializing static attributes;
When the class is loaded for the first time, execute the static code block first; When the class is loaded multiple times, the static code block is executed only once
It is executed when the class is initialized, not when the object is created
Non static members cannot be accessed directly in a static initialization block
Package
It is equivalent to the role of folders on files. It is used to manage classes and solve the problem of class name duplication
Two key points:
1. It is usually the first non annotative statement of a class
2. Package name: just write the domain name upside down and add the module name to facilitate internal management
Add packages when writing projects. Do not use the default package
com.gao and com gao. Car, these two packages have no inclusion relationship and are completely independent packages.
Only logically, the latter is part of the former
Import (import class)
If you want to use other package classes, you need to use import to call directly through the class name in this class. Otherwise, you need to write the full package name and class name of the class
be careful:
Java will import Java by default Lang, so we can use these classes directly
If you import two classes with the same name, you can only use package name + class name to display the calling related classes:
java.util.Date date = new java.util.Date();
java.util.*; Importing all classes under the package will reduce the compilation speed, but will not reduce the running speed
import static
Real jdk1 5. The newly added function is used to import the static properties and static methods of the specified class, so that we can directly use the static properties and static methods
package YuXiDemo; import static java.lang.Math.*;//Import all static properties of the Math class import static java.lang.Math.PI;//Import PI attribute of Math class public class TestStatic { public static void main(String[] args) { System.out.println(PI); System.out.println(random()); } }
encapsulation
Encapsulation is one of the three characteristics of object-oriented;
Packaging concept: high cohesion, low coupling
Advantages of encapsulation in programming:
Improve code security;
Improve code reusability;
"High cohesion": encapsulate details to facilitate modification of internal code and improve maintainability;
"Low coupling": simplify external calls, facilitate the use of callers, and facilitate expansion and cooperation
Three characteristics of object-oriented:
inheritance
-Subclass parent class
-Subclasses can inherit properties and methods from their parent classes
-Subclasses can provide their own separate properties and methods
encapsulation
-Hide some properties and methods
-Expose certain properties and methods to the public
polymorphism
-In order to adapt to various changes in requirements, the code becomes more general