Parameterized and nonparametric construction method, what is overload, the definition of overload,

Posted by pete07920 on Sat, 13 Nov 2021 01:24:07 +0100

1 what is a construction method

What is the construction method?

Constructor is a special method, which is a method with the same name as the class. The creation of an object is completed through a construction method, and its function is mainly to complete the initialization of the object. When a class instantiates an object, it will automatically call the constructor. Constructors, like other methods, can also be overloaded.

Nonparametric construction method

When creating a class, there will be a default parameterless constructor
For example, chestnuts:

    public Person(){
        System.out.println("I am the construction method");
    }

    public static void main(String[] args){
        Person a = new Person();
    }
    ```
![Insert picture description here](https://img-blog.csdnimg.cn/62549bf02deb40e7a0bdbdc1b04d7808.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5rGf5Y2X5rKh5pyJ5pif5pif,size_20,color_FFFFFF,t_70,g_se,x_16)
    It can be verified that parameterless methods are available by default and will be called by default when used to create objects


What is the construction method with parameters?

Having parameters, as the name suggests, is the construction method in which parameters are passed

For example, chestnuts:

public class Person {
    String name;
    int age;


	public Person(){
	        System.out.println("I am a parameterless construction method");
	    }
	public Person(String name){
	      this.name = name;
	      System.out.println("I have a parameter structure"+name);
	   }

    public static void main(String[] args){
//        No parameter
        Person a = new Person();
//        Have reference
        Person b = new Person("I'm Cece");

    }
}

Construction method with parameters new Object needs to pass parameters
>Person b = new Person("I'm Cece");

This sentence is to pass parameters. In fact, this is overload. Let's talk about overload:

2 overload

2.1 what is overloading?

The definition of overload is to expand on the basis of methods in the same class. Remember that the method name must be the same. The number of parameters and return types can be different. The above construction method is a special overload.

For example, chestnuts:

    public void say(){
        System.out.println("say");
    }

    public void say(int a){
        System.out.println(a);
    }

This is an overload of a method that has no return value

The following is an overload of a method with a return value:
Chestnuts:

public String sayyou(){
        return "you";
    }
public int sayyou(int b){
        return b;
    }

Easy! This is overloading

3 variables

3.1 static variables

Static variables belong to classes, also known as class variables.
Common variables (instance variables) belong to objects,
Static variables can be accessed by class name
Instance variables need to be accessed through objects

public class Class02 {
/**
 *@desc variable
 *
 * @author  wangxihao
 * @email wangxh0108@163.com
**/
    static int age;
    String name;

    public static void main(String[] args){

//        Assignment by class name
        Class02.age=18;

        Class02 class02 = new Class02();

        System.out.println(age);
    }

And static variables are variables shared by classes
For example, chestnuts:

package PersonObject;

/**
*@desc variable
*
* @author  wangxihao
* @email wangxh0108@163.com
**/
public class Class02 {

   static int age;
   String name;
   static  int ticket = 100;
   public void safe(){
       this.ticket = ticket-1;
   }

   public static void main(String[] args){
       Class02 class02 = new Class02();
       class02.safe();
       System.out.println("surplus"+ticket);
       Class02 class03 = new Class02();
       class03.safe();
       System.out.println("surplus"+ticket);

   }
}

It can be seen that the ticket of int is shared for class02 and class3. The tickets sold will be reduced by one.

4 static method

Static methods are methods modified by static
For example, chestnuts:

package PersonObject;

/**
 *@desc variable
 *
 * @author  wangxihao
 * @email wangxh0108@163.com
**/
public class Class02 {
    public static void say02(){
        System.out.println("This is a static method");
    }

    public static void main(String[] args){

        Class02 class02 = new Class02();
        class02.say01();
    }
}

4.1 non static methods can call static methods

 public void say01(){
        System.out.println("This is not a static method");
        say02();
    }

    public static void say02(){
        System.out.println("This is a static method");
    }


//Static variables can also be called, and ordinary methods can be called
   public void say01(){
        System.out.println("This is not a static method");
        ticket=50;
        name="nihao";
        say02();
    }

    public static void say02(){
        System.out.println("This is a static method");
        ticket=20;
//        say01();
 //       name:
    }

4.2 static methods cannot call non static methods

Calls to variables

4.3 summary of three sentences

Non static methods can be called for both ratio variables and methods.
Static methods can only call static variables and static methods

Topics: Java Back-end