Mutual calls of java constructors
public Student(int age){ super(); = age; } public Student(String name , int age){ /* super(); Note that super() can not be called here anymore because this(age); below calls the constructor above and already exists super();This constructor cannot be called again. So just writing this(age) is enough; */ this(age); = name; }
Other small details
When calling the parent constructor ( super(); ) or overload constructor ( this(); ) in the body of the constructor, it should be placed in the first line in the method body.
Construction method and its overload
1. Construction method
There is a special member method in a class whose method name is the same as the class name, which is called a constructor.
The creation of objects and the initialization of instance variables can be completed through the construction method.
Here is an example of a constructor:
public class Puppy{ public Puppy( ) { } public Puppy(String name) { //This constructor has only one name } }
Each class has a hidden parameterless constructor.
public class Dog{ /** * constructor with parameter * @param name * @param age */ public Dog(String name,int age){ = age; = name; } /** * No tragic constructor */ public Dog(){} String name; int age; public void ptint(){ ("name = "++";age = "+age); } public static void main(String[] args) { new Dog().ptint(); new Dog("Flower Flower",9).ptint(); } }
2. Initialization of construction methods and objects
How to call the constructor and which operator is used?
When an object is instantiated using the new operator, the system creates a memory area for the object and automatically calls the constructor to initialize the member variable.
Below is the new operator instantiating an object.
Puppy p = new Puppy( ); Puppy p1 = new Puppy(“Zhang San”);
3. Example - Initialize member variables using constructor methods
class Triangle { int x,y,z; public Triangle(int i,int j,int k) { x=i;y=j;z=k; public static bpplean judge(Triangle m) { if((*+*)= =(*)) //Refer to the sqrt() method of the Math class library return true; else return false; } public static void main(String args[]) { Triangle t1=new Triangle(3,4,5); if(judge(t1)) //Call the judge() method and determine that the member variable of t1 is //Can it form the three sides of a right triangle?("This is a right triangle"); else ("This is not a right triangle"); } }
4. Characteristics of construction method
- The constructor name and class name must be the same, and the upper and lower case must be the same.
- The constructor has no return value and the void type is not written.
- When creating an object, at least one constructor must be called.
- The constructor cannot be called explicitly directly.
- The main function of the construction method is to initialize the object.
- The modifier list is currently written uniformly: public. Never write public static.
5. Construct method overload
The constructor supports method overloading. There can be multiple constructors in a class. And all constructor names are the same.
For instance variables, as long as you do not manually assign it to the constructor, the unified value will be assigned by default. Default system value assignment.
Method overloading features:
- The overloaded method must be in the same class
- The method name is the same
- Different parameter list (not including different parameter names)
- Method overloading has nothing to do with access modifiers and return value types
6. Member variables
Properties in a class, that is, variables defined directly in the class, are called member variables, which are defined outside the method.
Local variables are variables defined in methods; generally speaking, local variables need to be assigned values before use, otherwise there will be compile errors.
The difference between member variables and local variables:
- Different scopes. The scope of a local variable is limited to the method that defines it, and it cannot be accessed outside the method; the scope of a member variable is visible inside the class, and all member methods can use it, and if access permissions allow it, member variables can also be used outside the class.
- The initial values are different. Member variables can not be initialized when defined, Java will assign initial values to member variables; however, Java will not assign initial values to local variables, so it must be initialized before use when defining local variables.
- In the same method, local variables with the same name are not allowed, and local variables with the same name can be present in different methods.
- Local variables can have the same name as member variables, and when used, local variables have higher priority.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.