1 construction method
1.1 concept
Constructor is a special method. It is a method with the same name as the class and the return value type is the class type with the same name. The creation of an object is accomplished through a construction method. Its function is mainly to complete the creation or initialization of an object. When a class instantiates an object, it will automatically call the constructor.
Constructors, like other methods, can also be overloaded.
1.2 form
You can have no or no parameters
Modifier class name([Parameters]){ Code }
1.3 creating object cases by construction methods
Two methods of creating objects
public class Test { String name; //When there is a construction method with parameters, nonparametric construction will no longer be generated automatically //public Test(){} //Parametric structure public Test(String n){ name=n; } void eat(){ System.out.println("Test.eat()"); } } class Test{ public static void main(String[] args) { //Commented out the parameterless construction can also run, indicating that the system will automatically provide a parameterless construction method Test t2 = new Test(); t2.eat(); //t is the reference variable, which refers to the address value of the object. //Find the object according to the address value and get the data of the object Test t = new Test("Zhang San"); System.out.println(t.name); } }
1.4 member variable assignment cases
public class Test{ String name; int age; //Assignment method 1: assign value through parameter containing construction method public Test(String name,int age){ this.name=name; this.age=age; } //Assignment method 2: assign values through setXxx() method public void setName(String name){ this.name=name; } public void setAge(int age){ this.age=age; } } class TestDemo{ public static void main(String[] args) { //Assignment method 1: assign values by parameterless construction method Test t1= new Test(); System.out.println(t1.name); System.out.println(t1.age); //Assignment method 1.1: assign values by parameter containing construction method Test t2= new Test("zhangsan",30); System.out.println(t2.name); System.out.println(t2.age); //Assignment method 2: assign values through setXxx() method Test t3= new Test(); t3.setName("rose"); t3.setAge(25); System.out.println(t3.name); System.out.println(t3.age); } }
2 construction code block and local code block
2.1 construction code block
1. Inside a class, outside a method, a block of code.
2. It is usually used to extract common code in construction methods.
3. The construction code block is called before each call to the construction method
4. Load prior to construction method
class c{ String country; { //Execute before construction method country="China"; } public c() { System.out.println("1 Contestant, from"+country); } public c(int a) { System.out.println("2 Contestant, also from"+country); } }
2.2 local code block
1. Code block inside method
2. It is usually used to control the scope of action of variables. It is invalid when there are parentheses
3. The smaller the range of variables, the better. Member variables have thread safety problems
4. Summary: execution sequence:
The construction of code blocks is the highest priority, and the local code blocks are executed in sequence
2.3 code block loading sequence case
public class TT { public static void main(String[] args) { Student s = new Student(); s.init(); } } class Student{ { System.out.println("Construct code block 1");//First execution } public void init(){ { System.out.println("Local code block");//Call init method to execute } } { System.out.println("Construct code block 2");//Second execution } }
3 this keyword
3.1 concept
This represents a reference object of this class.
In the constructor, this() must be placed on the first line.
3.2 form
//name=name; //age=age; Actually, I want to Student Local variable of class name Assign the value of to the member variable, which is equivalent to the operation you want: //Student.name=name; But you can't write the class name directly. In this case, use the object representing this class this To finish. The code becomes: this.name=name;
3.3 cases with the same variable names
Used to distinguish between local variables and member variables when they have the same name.
If there is a variable with the same name nearby, the proximity principle of the variable will be followed. How to call the member variable?
public class T { public static void main(String[] args) { Animal a = new Animal(); a.run("chinese rhubarb"); System.out.println(a.name); } } class Animal{ String name; /* //When the name of a local variable is different from that of a member variable, simply take the value of the local variable and assign a value to the member variable directly public void run(String n){ name=n; }*/ //Here's the problem. What happens when a local variable and a member variable have the same name?? public void run(String name){ //name=name;// The principle of using variables nearby is that they are used recently, that is, they have been using local variables this.name=name;//this calls the member variable } }
3.4 call cases between construction methods
public class T { public static void main(String[] args) { Animal a = new Animal(); // Animal a2 = new Animal("Wangcai"); } } class Animal{ public Animal(){ this("Laifu");//Call the parameterized construct in this class } public Animal(String name){ System.out.println(name); } }