Java Construction Method: The method of parameter construction and the method of non-parametric construction
Non-parametric construction method
1. Definition:
If you write a javabean without adding an parameterless construction method, the compiler automatically adds the parameterless constructor method;
However, if you add a parameter constructor without adding an parameterless construct method when you write, the compiler will only recognize the parameter construct method without adding the parameterless constructor by default.
So, if you need to use the parameterless construct method, be sure to add it inside the class
2, for example:
(1) There are 4 JavaBean--------> Noparaminfo.java, Noparaminfo2.java, Noparaminfo3.java, Noparaminfo4.java
public void noparaminfo{
private String name;
Private String adrress;
}
public void noparaminfo2{
private String name;
Private String adrress;
Public Noparaminfo () {}//parameterless construction method
}
public void noparaminfo3{
private String name;
Private String adrress;
A method of parametric construction
Public Noparaminfo (String name,string address) {
This.name=name;
this.address=address;
}
}
public void noparaminfo4{
private String name;
Private String adrress;
Public Noparaminfo () {}//parameterless construction method
A method of parametric construction
Public Noparaminfo (String name,string address) {
This.name=name;
this.address=address;
}
}
(2) test
@Test
public void Testnoparaminfo () {
In this case, the compiler does not report an error
Noparaminfo np=new noparaminfo ();
Write this, the compiler will not complain
NoParamInfo2 np=new NoParamInfo2 ();
In this case, the compiler will report an error, because the parameterless construction method is overridden by a parameter constructor, and the compiler cannot provide a parameterless construction method
NoParamInfo3 np=new NoParamInfo3 ();
In this case, the compiler does not report an error
NoParamInfo4 np=new NoParamInfo4 ();
In this case, the compiler does not report an error
NoParamInfo4 np=new NoParamInfo4 ("Tina", ' Global Village ');
}
two, the method of parametric construction
as the name implies, there are some formal parameters added to the parameterless construction method.