Some Notes on Constructors/Constructors

  • Default Constructor (Constructor): No parameter list. What it does is create a "default object". If you do not actively write a constructor, the compiler will automatically create a default no-argument constructor for you. The constructor is required, without it, there is no method to call and no object can be created.
  • The name of the constructor is the same as that of the class, no modification, and no return value.
  • Constructors can be overloaded. as follows:
     1 class Bird{
     2     Bird(int i){}
     3     Bird(double d){}
     4 }
     5 
     6 public class NoSynthesis {
     7     public static void main(String[] args) {
     8     Bird b1=new Bird2(1);
     9     Bird b2=new Bird2(1.0);
    10 //    Bird b3=new Bird();             Wrong!!!!!!!
    11     }
    12 }

    The original constructor Bird() of the class Bird has been overloaded twice, with different parameters each time, but the original no-parameter default constructor no longer exists. So new Bird() is a wrong way of writing, either taking an int parameter or a double parameter.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325135607&siteId=291194637