Lesson 8 (constructor and Its Features)
Constructor:
A constructor is a function called when an object is created. It initializes the object.
Features:
1. The function name and class name are the same
2. the return value type does not need to be defined.
3. No specific return value
Purpose: Initialize the object.
Note:
1. Features of default constructor
2. Multiple constructors exist in the form of overloading
Code Demonstration:
Class man {private int age; private String name; man () // defines a man class constructor {System. out. println ("sun");} public void fun () {System. out. println (name + "" + age) ;}} public class Main {public static void main (String [] args) {man jo = new man () ;}} print sun,
PS: If no constructor has been defined in a class, a default null constructor will appear in the class.
If the specified constructor is defined in the class, the default constructor in the class will no longer exist.
Note: If a constructor with parameters is defined, the constructor is also constructed by default and will not be automatically generated by the compiler.
Actually: the default null Parameter Function is man (){}
For example, a class:
Class test
{
}
There is nothing, but there is actually a test () {} in it. during compilation, the compiler will first determine whether to write the constructor. If no constructor is automatically added, it will delete the default constructor.
The constructor is called when an object is created.
Differences between common and constructor functions:
1. constructor: when an object is created, it calls the corresponding constructor and initializes the object.
General functions: After an object is created, it is called only when the function is called.
2. constructor, called only once when an object is created
General functions: After an object is created, it is called several times if you want to call it several times.
When can I use constructors?
When describing a thing, it contains some content, which is defined in the constructor.
For example, when an object is created, it has a default age and name.
Sample Code:
Class man {private int age; private String name; man () // defines a man class constructor {age = 20; name = "john" ;}public void fun () {System. out. println (name + "" + age) ;}} public class Main {public static void main (String [] args) {man jo = new man (); jo. fun ();}}
Reload the constructor:
Class man {private int age; private String name; man () // defines a man class constructor ------------- | {| age = 20; | name = "john "; |}| // The object has an age and name before it is created | man (int a, String s) | --------> heavy load {| age = a; | name = s; |} | man (String s) | {| name = s; |} --------------- | public void fun () {System. out. println (name + "" + age) ;}} public class Main {public static void main (String [] args) {man jo = new man (); jo. fun (); man jj = new man (1, "BLF"); jj. fun (); man jjj = new man ("BLF2"); jj. fun (); // print BLF 0 }}
PS: A function name with the same name appears in a class, which must be reloaded.
If the constructor is not called when an object is created, the creation fails.
The memory distribution diagram of the constructor (for example): objects need to be added to the heap, and functions need to be added to the stack.
Constructor details:
To facilitate future modification of information, add the set
You cannot add void or so on before the name of the constructor. After the constructor is added, it is not a constructor, but a general function (constructor features 2)
Error code example
Class man {private int age; private String name; man (int a) {age = a ;}// the object has an age before it is created. The name is man (int, string s) {age = a; name = s; fun ();} public void set (String s) {name = s;} public void fun () {System. out. println (name + "" + age) ;}} public class Main {public static void main (String [] args) {man jo = new man (); jo. set ("BLF2"); jo. fun ();}}
The problem with this code is:
Because no constructor is defined.
The default constructor is automatically added when the Code does not create a constructor. However, the above Code has constructor, but no man () {} Type constructor is found. Therefore, an error is returned.