Structure
Notes on the construction method
① Creation of construction method
If no construction method is defined, the system will give a default no-parameter construction method. If a construction method is defined, the system will no longer provide a default construction method.
② Overloading of the construction method
If you have customized a parameterized construction method, but also use a parameterless construction method, you must write another parameterless construction method
③Recommended use
Regardless of whether it is used or not, hand-written no-parameter construction methods
Standard production
①Member variables
Use private decoration
②Construction method
Provide a parameterless construction method
Provide a construction method with multiple parameters
③Membership method
Provide setXxx0/getX0x0 corresponding to each member variable
Provide a show0 that displays object information
4. Two ways to create an object and assign values to its member variables
.Use setXox0 to assign value after creating the object with no parameter construction method ●Use the parameter construction method to directly create an object with attribute values
package stent;
public class stentt {
private String name;
private int age;
public stentt() {
}
public stentt(String name) {
this.name = name;
}
public stentt(String name,int age) {
this.name = name;
this.age = age;
}
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;
}
}
package stent;
public class stenty {
public static void main(String[] args) {
stentt s= new stentt("身上");
s.setAge(60);
System.out.println("我是"+s.getName()+"bb"+s.getAge()+"售后");
System.out.println("==============");
stentt s2= new stentt("网点",20);
System.out.println("我是"+s2.getName()+"bb"+s2.getAge()+"售后");
s2.setAge(21);
System.out.println("我是"+s2.getName()+"bb"+s2.getAge()+"售后");
}
}