1. Construction method
Definition: A method that has no return value with the same name as a class is called a construction method;
public class Test1 {
private String name;
private int age;
Public Test1 () {
}
}
The Test1 () above is the default constructor, which is automatically generated even if no Java Virtual machine is defined at run time.
Of course, if you define the overloaded construction method, it will not be generated automatically;
The construction method has two points 1. Construct an object by constructing method, 2. The initial value can be quickly assigned by constructing method;
public class Main {
public static void Main (string[] args) {
Test1 t1=new test1 (); The//new is followed by a construction method to create an object
}
}
And if there are other attributes in the class, there can be overloads of the constructor method:
Public Test1 (String name, int.) {
THIS.name = name;
This.age = age;
}
The overloaded construction method can be used to assign the initial value to the object very quickly, especially for the private property, if it is inconvenient to use getter and setter.
public static void Main (string[] args) {
Test1 t1=new test1 ();
Test1 t2=new test1 ("Bob", 18);
SYSTEM.OUT.PRINTLN (t1);
System.out.println (T2);
}
Output Result:
test1 [Name=null, Age=0]
test1 [Name=bob, age=18]
T1 does not assign an initial value all of its values are the system settings of the initial;
T2 using the constructive method of the parameters, directly to the attribute set the initial value, very convenient, do not use setter method to set values;
2. This
Who calls the object that points to the call;
To illustrate:
Add the following fly () method to the Test1 class above:
public void Fly () {
System.out.println (this.name+ "will Fly");//this points to the calling object
}
Called in the Run:
public static void Main (string[] args) {
Test1 t1=new test1 ();
Test1 t2=new test1 ("Bob", 18);
T1.fly ();//The This in the method points to T1
T2.fly ();//The This in the method points to T2
SYSTEM.OUT.PRINTLN (t1);
System.out.println (T2);
}
The results are as follows:
Null will fly
Bob can fly.
Another thing to note about this is that it is used in the construction method:
Public Test1 (String name, int.) {
This ();//The constructor method must be called without parameters before the constructor is run for property assignment.
THIS.name = name;
This.age = age;
}
Of course, the construction method call mainly look at this (parameter list) in the parentheses inside the parameter list and which constructor method is the same as the execution of which construction method;
2. Super: Point to the parent class, calling the parent class's properties and methods;
Actually use the same as this keyword;
Public Test1 () {
Super ();
}
The top super (() calls the constructor of the parent class, and if it does not inherit the parent class, its parent is pointing to object (the superclass of all classes);
Use: Suiper keyword plus ". "You can access the properties and methods that are accessible to the parent class and are generally used to distinguish between the overridden methods of the quilt class;
Considerations for using the This (parameter list) and super (parameter list) construction methods:
A. Can only be located on the first line of the construction method;
B. Cannot appear in a construction method at the same time;
C. The constructor of the parent class is always accessed first, and the method of constructing the child class is accessed.
Construction methods in Java, usage of this, super