SoFunction
Updated on 2025-03-08

Detailed explanation of the principles and usage of Java overload construction

This article describes the principles and usage of Java overload construction. Share it for your reference, as follows:

Method with parameters

【1】No parameter, no return value

void Method name(){Method body;}

【2】No parameters, return value

int Method name(){Method body;}

【3】There are parameters, no return value

void Method name(int num){Method body;}

【4】There are parameters, there are return values

int Method name(int num){Method body;}

The difference between variable parameters and array parameters

【1】Variable parameters are flexible to transfer parameters, can be without parameters, can be multiple parameters, can be arrayed;

Array parameters can only be passed to arrays

【2】Variable parameters must be placed at the end

Arrays can be placed anywhere

【3】There can only be one variable parameter;

There can be multiple array parameters.

Recursive algorithm

Recursion: The program calls its own algorithm.

Conditions: 1. Call the method itself.

2. Export

Comparison of nested for loops and recursive implementation

The stack is mainly used to store stack frames. Every time a method is executed, a stack pressing operation occurs. Therefore, when recursion is used, there are more stack frames generated, and recursion will affect memory and consume a lot of memory. However, using a for loop, a method is executed. Pushing the stack frame once, only one stack frame exists, so it saves memory.

Package

Encapsulation definition: hides the properties and implementation details of the object, and only provides public access methods to the outside world.

Benefits of using package

1. A good package can reduce coupling.

2. The structure inside the class can be freely modified.

3. More precise control of members can be provided.

4. Hide information and implement details.

public class Husband {
  
  /*
    * Encapsulation of attributes
    * A person's name, gender, age, and wife are all private attributes of this person
    */
  private String name ;
  private String sex ;
  private int age ;
  private Wife wife;
  
  /*
    * Setter() and getter() are interfaces for the object to be developed externally.
    */
  public String getName() {
    return name;
  }

  public void setName(String name) {
     = name;
  }

  public String getSex() {
    return sex;
  }

  public void setSex(String sex) {
     = sex;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
     = age;
  }

  public void setWife(Wife wife) {
     = wife;
  }
}

Method overloading

In a class, the method names are the same and the parameter list (number, order, type) is different. Nothing to do with the return value.

Function: Convenient to memory and use.

Different ways to overload methods

There are three ways to overload methods in java, they are:

  • By changing the number of parameters
  • By changing the data type
  • By changing the order of parameters

Note: In Java, it is not possible to implement method overloading only by changing the return type of the method.

Constructor

The system will automatically create a constructor (constructor) default construct.

If there is no defined constructor displayed, the system will automatically define a constructor called the default constructor.

You can display the definition constructor. As long as the definition constructor is displayed, the system calls the display definition constructor.

No parameter construct, can only be called when creating an object

Note: 1. No return value

2. The name and class name must be the same

Function: Initialize the object.

The difference between constructor and accessor

1.Constructor After creating the object, all attributes are initialized.

The accessor cannot.

2. Constructor Initializes the object when creating it, and can initialize all attributes at once.

The accessor needs to call the assignment one by one after creating the object.

3. After creating an object, if you want to modify the attribute value, you need to use an accessor.

The difference between a constructor and a normal method

1. The constructor is used to initialize the object.

The ordinary method is to complete a specific function

2. The constructor can only call new when creating an object.

Ordinary methods can be called at will when used, and they will not be executed without calling them.

Initialize the object

1. Member variables are initialized by default

2. Initialize at the declaration or initialize the building block

3. Constructor initialization

Building blocks

Definition: in class

class Class Name{

  {
    Building blocks:
    effect:Solve that the attribute values ​​in the constructor are the same,Code reuse issues。
  }

}

This class constructor call

this();//

For more information about Java algorithms, please check out the topic of this site:Introduction and Advanced Tutorial on Object-Oriented Programming in Java》、《Java Data Structure and Algorithm Tutorial》、《Summary of Java operating DOM node skills》、《Summary of Java files and directory operation skills"and"Summary of Java cache operation skills

I hope this article will be helpful to everyone's Java programming.