Constructor
Constructor is part of the class.
Role: initialize an object and returns the address of the object.
构造器的格式:
修饰符 类名(形参列表){
}
- Modifiers: stage with all public.
- Class Name: The name of the constructor must be the name of the current class, otherwise an error!
- The constructor can not declare the return value type, default return address of an object reference.
- Parameter list: with the same method. (Data type parameter variable 1, variable data type parameter 2, ...)
Constructor Category:
- No argument constructor.
- There constructor parameter.
Action constructor : initialize an object and returns the address of the object.
Constructor initializes the object format : class name = new Object Name constructor;
Builder considerations:
- Just write a class, it will default comes with a no-argument constructor.
- But if he wrote a constructor have parameters, the default no-argument constructor disappeared at this time require the programmer to write a no-argument constructor to use it.
Examples of non-argument constructor:
public class ConstructorDemo01 {
public static void main(String[] args) {
// 初始化对象
Wolf qpl = new Wolf();
System.out.println(qpl);
}
}
class Wolf{
// 1.无参数构造器。
public Wolf(){
System.out.println("无参数构造器被触发指向,返回对象的地址~~");
}
}
There is overloaded constructor:
can be provided with a constructor parameter can also provide a no-argument constructor for the class, we call them exist as overloaded constructor.
No role configuration parameters:
Object to initialize a class returns inside the member variables default values. ( In general programming device must be provided constructor with no arguments )
There are construction parameters of the role:
object initialization of a class returns can be assigned immediately to the member variable objects in the object initialization time. Animal taiTi = new Animal ( "Teddy", 2, 'male'); (immediate Assignment)
public class AnimalDemo {
public static void main(String[] args) {
// 1.调用无参数构造器初始化一个对象返回:对象都是采用默认值。
Animal jinMao = new Animal();
jinMao.setName("金毛");
jinMao.setAge(2);
jinMao.setSex('公');
System.out.println(jinMao.getName());
System.out.println(jinMao.getAge());
System.out.println(jinMao.getSex());
jinMao.run();
// 2.调用有参数构造器初始化一个对象返回:对象都会直接赋值。
Animal taiTi = new Animal("泰迪",2 , '公');
System.out.println(taiTi.getName());
System.out.println(taiTi.getAge());
System.out.println(taiTi.getSex());
taiTi.run();
}
}
class Animal{
// 1、成员变量(属性): 建议私有。
private String name ;
private int age ;
private char sex;
// 3、无参数构造器默认的。(必须提供的)
public Animal() {
System.out.println("无参数构造器被执行~~~");
}
// 4、有参数构造器(建议提供)
public Animal(String name, int age, char sex) {
System.out.println("有参数构造器被触发执行~~~~");
this.name = name;
this.age = age;
this.sex = sex;
}
// 5.普通方法
public void run(){
System.out.println("名字是:"+name+",年龄是:"+age+" , 性别是:"+sex+" , 跑的贼溜~~~~~");
}
// 2、getter+setter方法:暴露成员变量的取值和赋值。(自动生成)。
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
}
Note:
No argument constructor argument constructor and there is in the programming can be generated directly by the tool
Right-click -> select Generate-> select Constructor-> Hold down the Shift key and click on the last one-and-click direct all OK is generated there argument constructor, then click Select None is generated, no arguments constructor.