Learning from 0 Development Notes-Construction Method

Learning from 0 Development Notes-Construction Method

  1. Construction method : The construction method is a method specially used to create an object. When we create an object through the keyword new, we are actually calling the construction method.
//构造方法的格式
// public 类名称(参数类型 参数名称) {
    
    
// 	方法体
//}

public class Student {
    
    
	public Student() {
    
    
		System.out.printlin("构造方法执行了");
	}
}
  1. Precautions
    • The name of the constructor must be exactly the same as the name of the class it is in, even the case must be exactly the same
    • Do not write the return value type in the constructor
    • The constructor cannot return a specific return value
    • If no construction method is written, the compiler will give a construction method by default, without parameters and method body, and do nothing.
    • Once at least one constructor is written, the compiler will no longer give away
    • The construction method can also be overloaded (the method name is the same and the parameter fission is different)
  2. Function
    Writing the construction method can be used to initialize member variables and other information (passing parameters).

Guess you like

Origin blog.csdn.net/weixin_42595206/article/details/112919707