The difference between member method and construction method in Java**

the difference

The following will analyze the difference between member methods and construction methods from the purpose, format, code segment, and call :
(Purpose: better understand and learn Java, and deepen the understanding and use of member methods and construction methods)

A. Purpose:
Member methods : generally implement operations on member variables in the class , provide certain functions, and have return types , which can be void types.
Construction method : initialize the data of the object without return value.

B. Format:
member method : modifier public return value type method name (parameter type parameter name 1, parameter type parameter name 2...) { method body; return return value; }



Two clear methods of the method:
A: clear return value type The data type of the function result
B: The parameter list clearly has several parameters, and the type
construction method of the parameters :
A: The method name is the same as the class name
B: There is no return value type, even void cannot be written
C: No specific return value

C: Code snippet:

package Object;
public  class Student {
    
    
    private  String name; //成员变量
    private String age; //成员变量
    public Student(String name){
    
     //构造方法
        this.name = name;
    }
 
    public String getName() {
    
     //成员方法
        return name;
    }
 
    public void setName(String name) {
    
    //成员方法
        this.name = name;
    }
 
 
    public String getAge() {
    
    //成员方法
        return age;
    }
 
    public void setAge(String age) {
    
    //成员方法
        this.age = age;
    }
}

D: Call:


package Object;
public class StudentDemo {
    
    
    public static void main(String[] args) {
    
    
        Student s = new Student("Tony");//构造方法调用
        System.out.println(s); 
        s.setName("Lisa");//成员方法调用
        System.out.println(s.getName());
    }
   //  构造方法通过new运算符调用,成员方法通过对象调用。

to sum up

Summary: The class name is the same as the construction method. This method is the construction method.
The methods in this class except the construction method are all member methods. The reason is that all the methods in the class except variables are basically member methods.
(If there is something wrong, please point it out)

Guess you like

Origin blog.csdn.net/m0_52646273/article/details/114027231