Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Java overload Construction

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis of Java overload construction, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Method with parameters

[1] No parameters, no return value

Void method name () {method body;}

[2] No parameter, return value

Int method name () {method body;}

[3] there are parameters and no return value

Void method name (int num) {method body;}

[4] there are parameters and return values

Int method name (int num) {method body;}

The difference between variable parameters and array parameters

[1] variable parameters can be passed flexibly, can have no parameters, can have multiple parameters, can be array

Array parameters can only pass arrays

[2] variable parameters must be placed at the end

The array can be placed anywhere.

[3] there can be only one variable parameter

There can be more than one array parameter.

Recursive algorithm

Recursion: the program calls its own algorithm.

Conditions: 1. Call the method itself.

two。 Exit

Comparison between nested for Loop and Recursive implementation

Stack is mainly used to store stack frames, each execution of a method will appear stack operation, so the use of recursion when the stack frame is more, recursion will affect memory, very memory consumption, and the use of for loop on the implementation of a method, press into the stack frame once, there is only one stack frame, so save memory.

Encapsulation

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

Benefits of using encapsulation

1. Good packaging can reduce coupling.

2. The internal structure of the class can be modified freely.

3. Members can be more precisely controlled.

4. Hide information and implement details.

Public class Husband {/ * * Encapsulation of attributes * A person's name, sex, age and wife are all private attributes of this person * / private String name; private String sex; private int age; private Wife wife; / * * setter (), getter () are the external development interfaces of the object * / public String getName () {return name;} public void setName (String name) {this.name = name;} public String getSex () {return sex } public void setSex (String sex) {this.sex = sex;} public int getAge () {return age;} public void setAge (int age) {this.age = age;} public void setWife (Wife wife) {this.wife = wife;}}

Method overload

In a class, the method name is the same, but the parameter list (number, order, type) is different. Has nothing to do with the return value.

Function: easy to remember and use.

Different ways of overloading 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 overload a method simply by changing its return type.

Constructor

A default constructor (constructor) is automatically created.

If there is no definition constructor shown, the system automatically defines a constructor called the default constructor.

You can display the definition constructor, as long as the display defines the constructor, then the system calls the constructor of the display definition

No-parameter construction, which can only be called when the object is created

Note: 1. There is no return value.

two。 The name and class name must be the same

Function: initializes the object.

The difference between constructor and accessor

1. After the constructor has created the object, all the properties are initialized,

The accessor cannot.

two。 Initialized when the constructor creates an object, it can initialize all properties at once

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

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

The difference between a constructor and a common method

1. The constructor is used to initialize the object.

The common method is to perform a specific function.

two。 The constructor can only call new when the object is created

Ordinary methods can be called at will when in use, and will not be executed if they are not called.

Initialize object

1. Member variables are initialized by default

two。 Initialization at declaration or building block initialization

3. Constructor initialization

Tectonic block

Definition: in class

Class class name {{Building Block: function: solve the problem of the same attribute value and code reuse in the constructor. }}

This class constructor calls the

This (); / /

Thank you for reading this article carefully. I hope the article "sample Analysis of Java overloaded Construction" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report