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

How to realize java method overloading

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "java method overload", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "java method overload how to achieve" article.

1. Concept

Let classes handle different types of data in a unified way. When you call a method, you decide which method to use by passing them different numbers and types of parameters, which is the use of polymorphism.

2. Characteristics

(1) it must be more than two methods of the same name.

(2) the combination of parameters between methods must be different (the number or type of parameters is different)

(3) the return value of the method can not be used as the basis for judging whether there is an overload between the methods.

3. Examples

Suppose you want to implement a method of summing numbers in the program, because the number and type of numbers involved in the summation are uncertain, so different methods should be designed for different situations. Then through a case to achieve the function of adding two integers, adding three integers and adding two decimals.

Public class MethodDemo02 {public static void main (String [] args) {/ / the following is the call to the summation method int sum1= add01 (1,2); int sum2= add02 (1,2,3); double sum3= add03 (1.2,2.3); / / the following code is the result of printing the sum System.out.println ("sum1=" + sum1); System.out.println ("sum2=" + sum2); System.out.println ("sum3=" + sum3) } / / the following method implements the addition of two integers public static int add01 (int x, int y) {return x + y;} / / the following method implements the addition of three integers public static int add02 (int x, int y, int z) {return x + y + z;} / / the following method implements the addition of two decimals public static double add03 (double x, double y) {return x + y } the above is the content of this article on "how to achieve java method overloading". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

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