Construction Method
First, construction method considerations
1. The name of the construction method must be the same as the name of the class I am in, even the case.
2. Construction method do not write the return value type, not even void.
3. The construction method cannot return a specific return value.
4. If no construction method is written, the compiler will default to a constructor method, without parameters, and the method body does nothing.
5. Once you have written at least one of the construction methods, the compiler will no longer be presented.
6. The construction method can also be overloaded.
The purpose of the construction method is to facilitate the assignment of the properties of the object, you can skip setter statements, but, later want to change the attributes, or the setter method cannot escape.
Second, the format of the method of construction
1 Public class name (parameter list) {2//dosomething3 }
Iii. How to assign values to member variables
Method One: Object name. member Variable name = new value
Deprecated, because after learning the encapsulation, all member variables are decorated with private, which is not available.
Way two: by setter method
Student stu = new Student ();
Stu.setname ("Zhang San");
Way three: Through the method of constructing a parameter.
Student stu = new Student ("Zhang San", 30);
四、一个 Standard classes should have those things
1 public testa{ 2 // 3 4 // 5 6 7 8 // 9 10 // getter method and Setter method 11 12 }
Five, quick to generate a standard class skills
In idea, you can select Code->generate directly in the title bar or press the shortcut key Alt+insert directly.
+constructor Generation Construction Method
+getter & Setter Generation GetXXX and Setxxx methods
Such a standard class can be generated quickly.
Java Construction methods