[object oriented three of JAVA knowledge points]

1. Overview and format of constructor

A. Overview and function of structural method

  • Initialize the data (properties) of an object

B. Format characteristics of construction method

  • Method name is the same as class name (size is the same as class name)
  • No return value type (neither void)
  • No specific return value (return) for ju't
class Demo_constructor{
public static void main(String[] args){
Person p=new Person();//The system calls the constructor as soon as the object is created
//p.Person(); / / constructor cannot be called with object
p.show();

}
 }







class Person{
private String name;
private int age;

//Construction method
public Person(){

System.out.println(" ");
//Return; / / the constructor also has a return statement

name="Zhang San;
age=12;
}
public void show(){
System.out.println(name+" "+age);

}

  }

2. Heavy load of construction method and precautions

A. Case demonstration

  • Overload of construction method
  • Overload: the method name is the same, independent of the return value type (the construction method has no return value), only the parameter list

B. Notes on construction method

  • If we don't give the gou 'construction method, the system will automatically provide a nonparametric construction method
  • If the construction method is given, the system will no longer provide the default nonparametric construction method
class Demo_constructor{
public static void main(String[] args){
Person p1=new Person();
p1.show();
System.out.println("Empty parameter structure ");
Person p2=new Person("Zhang San",23);
p2.show();

}
 }



class Person{
private String name;
private int age;

//Construction method
public Person(){

System.out.println("Empty parameter structure ");
}

public Proson(String name,int age){
this.name=name;
this.age=age;
System.out.println("Parametric structure ");
}


public void show(){
System.out.println(name+""+age);
}
}

3. Difference between two ways of assigning value to member variable

A.setxxx() method

  • Modify property value

B. Construction method

  • Initializing properties in objects

In the development of these two ways, setxxx is used more, because it is more flexible

class Demo_Person{

public static void main(String[] args){
Person p1=new Person("Zhang San",23);
//p1=new Person("Zhang Tianyi, 25); / / in this way, the running result seems to change the name to turn the original object into garbage
System.out.println(p1.getName()+""+p1.getAge());

Person p2=new Person();//Empty parameter construction creating objects
p2.setName("Li Si");
p2.setAge(24);
p2.setName("one by one")
System.out.println(p2.getName()+""+p2.getAge());

}
 }



class Person{
private String name;
private int age;

//Construction method
public Person(){

System.out.println("Empty parameter structure ");
}

public Proson(String name,int age){
this.name=name;
this.age=age;
System.out.println("Parametric structure ");
}
public void setName(String name){
this.name=name;
}
public void getName(){
return name;
}
public void setAge(int age){
this.age=age;
}
public void getAge(){
return age;
}

public void show(){
System.out.println(name+""+age);
}
}


Difference:

Construction method:

Posted on Fri, 31 Jan 2020 00:10:25 -0800 by pelleas1022