Meaning that it can create the constructor method of the object on mass participation (new) time.
As an ordinary function requires a variable name. When the name attribute assignment, the constructor has been assigned to get rid of the problem.
Constructor does not return type, method to create the object to an end is finished, there is no return value.
The constructor needs method name and the same class name
There may be a plurality of class constructors, the plurality constructor overload is present in the form of
Constructor can be private modified role: other programs can not create objects of that class. Getxxx is preferred to add the package and setxxx
Constructor definition:
note:
1. constructor method name needs class name and the same
The class name person, the following constructors are identical
public class person(){
private string name;
private int age;
The first: the constructor with no arguments
public person(){
system.out.in ( "La La La La");
}
The second: constructors this parameter representative of this class of objects, i.e. of the class person herein
public person(String name,int age){
this.name=name;
this.age=age;
Because the same name two person method, but because the second function tangible parameters, so it comes to method overloading, (parameter type, location, a different number), solved this keyword before this method is used to solve the member variables and local variables are duplicate names.
}
Third: If we do not write a constructor, but was able to test new a new object in the class, because of a default constructor
}
Test: constructor can only be transferred once, but the general approach can be retrieved multiple times
{class CS_D public
public static void main (String [] args) {
// new primary modulation time
Demo01 D = new Demo01 (); i.e. when the new object constructor is called the construction, method object name {name (). } acquired entirety construction method, and the method can be configured to pass parameter
Demo01 d1 = new Demo01 ( "Ian", 23);
Values apply only once, if the need to change the need to modify the package setXXX
System.out.println (d.getName () + "... " + d.getAge ());
Output is called with an ordinary method
System.out.println (d1.getName () + "... " + d1.getAge ());
You can always use the general category
d.person();
To call
}
}
this keyword:
This object is representative of this class
this ( parameter list );
It represents in the constructor
this. Normal retrieval methods and properties
this (parameter list); constructor must be placed in the first row perform
public class student(){
string name;
int age;
Retrieved parameters to the constructor has assigned
the this ( "Joe Smith", 23);
public student(String name,int age){
This two equivalent, is the same this.name = Zhang; this.age = 12;
Here this is equal to the present class object (member variables) name = Joe Smith; age = 12;
this.name=name;
this.age=age;
}
}
Test: Because this call assignment on the inside, so we only need to output
public class Cs_student {
public static void main(String[] args) {
Student stu=new Student();
Student stu1=new Student();
stu1.Student();
System.out.println(stu.getName()+"..."+stu.getAge());
}
}