Java 中的函数重载是指存在具有相同名称但传递给它的参数数量不同的函数,这些参数的数据可能不同,如 int、double、float,并且用于返回不同的值,并且在各自的重载内部计算方法。函数重载用于通过涉及更多隔离的函数来降低复杂性并提高程序的效率,这些函数可用于根据各自的功能来区分彼此。重载函数与编译时或多态性相关。还有一个类型转换的概念,基本用在重载函数中,用来计算变量中类型的转换。
语法
广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试重载函数具有相同的名称,但分配给它们的参数或参数类型不同。它们可用于计算方法中分配的变量数量内的数学或逻辑运算。重载函数的语法如下所示,其中最多分配了 N 个变量。
public class OverloadedMethod { public int FunctionName(int x, int y) //Two parameters in the function { return (x + y); //Returns the sum of the two numbers } // This function takes three integer parameters public int FunctionName(int x, int y, int z) { return (x + y + z); } // This function takes two double parameters public double FunctionName(double x, double y) { return (x + y); }//Many more such methods can be done with different number of parameters // Code used to input the number and public static void main(String args[]) { FunctionName s = new FunctionName(); System.out.println(s.FunctionName(10, 20)); System.out.println(s. FunctionName(10, 20, 30)); System.out.println(s. FunctionName(10.5, 20.5)); } }
函数重载的工作原理
函数重载的工作原理是调用具有相同名称但传递给它的参数数量不同的不同函数。有许多编码示例可以显示,以便正确识别函数重载的优点和缺点。
函数重载的优点
函数重载具有相同的名称。因此,我们不必创建与各自函数内完成的工作具有相同功能的方法。该功能不仅解决了命名冲突的问题,还提高了程序的可读性。
函数重载示例
以下示例如下:
示例#1
在编码示例1中,我们将看到重载的main(),它用于在面板中显示不同的输出,并展示重载的main()在编程语言中如何使用以及不同函数的调用如何产生不同的输出,分别。
代码:
//Java program with overloaded main() import java.io.*; public class Test { // First main method which is created public static void main(String[] args) { System.out.println("Main function called"); Test.main("Everyone"); } // First overloaded main method public static void main(String ar1) { System.out.println("Hi, " + ar1); Test.main("Hello Everyone", "Buenos dias"); } //Second overloaded main method public static void main(String ar1, String ar2) { System.out.println("Mi todo eres tu, " + ar1 + ", " + ar2); } }
当 main() 被调用时,会在输出面板中显着地显示输出,如下所示。首先,调用并打印main()的打印语句。然后,通过区分参数的数量,程序将代码段中存在的两个重载函数分开。 main() 调用第一个重载函数,而第一个重载函数调用第二个重载函数。
输出:
示例#2
在第二个编码示例中,我们将看到执行乘法的函数,但分配给它们的参数数量不同。这将帮助人们开发用于处理不同参数的不同函数。
代码:
class Adder { static int multiply(int a,int b) { return a*b; } static int multiply(int a,int b,int c) { return a*b*c;} } class TestOverloading1 { public static void main(String[] args) { System.out.println(Adder.multiply(110,110)); System.out.println(Adder.multiply(110,110,110)); } }
输出:
示例 #3
在此编码示例中,我们将看到具有相同名称和相同数量参数的函数,但它们有一个功能可以作为它们之间的区别。功能上的区别在于函数中存在的数据类型,一个是整数,另一个是双精度。
代码:
//Program to add numbers distinguishing on the datatype of parameters class Number { static int add(int a, int b)//Both integer variables { return a+b;//Returning the sum } static double add(double a, double b)//Both double variables { return a+b;//Returning the sum } } class TestOverloading2 { public static void main(String[] args) { System.out.println(Number.add(1,110)); System.out.println(Number.add(12.4,18.8)); } }
在上面的程序中,我们在计算两个整数值之和时输入两个整数值。输入的数字是 10 和 20。输出应该给出值 30,这将按原样打印总和。另外,当我们输入两个 double 值时,会打印总和,这是第二个重载函数。我们输入双精度数据类型变量的值15.5和16.3,然后我们最终看到的结果是31.8,这是总和。我们将在下面的屏幕中看到程序的输出面板。
输出:
结论
在本文中,我们看到了重载函数的不同功能和概念,它们具有相同的名称,但参数数量不同或数据类型不同。我们还注意到需要如何执行重载函数的优点和语法。在本文中,重载函数主要用于乘法和加法,这对整个编程概念进行了详细介绍。
以上是Java 中的函数重载的详细内容。更多信息请关注PHP中文网其他相关文章!

Java在不同操作系统上的表现存在细微差异。1)JVM实现不同,如HotSpot、OpenJDK,影响性能和垃圾回收。2)文件系统结构和路径分隔符不同,需使用Java标准库处理。3)网络协议实现差异影响网络性能。4)GUI组件外观和行为在不同系统上有别。通过使用标准库和虚拟机测试,可减少这些差异的影响,确保Java程序稳定运行。

javaoffersrobustobject-IentiendedProgrammming(OOP)和Top-Notchsecurityfeatures.1)OopinjavainCludesClasses,对象,继承,多态性,和列出,andeclingfleximaintainablesys.ss.2)SecurityFeateTuersLudEtersludEterMachine(

JavaScriptandJavahavedistinctstrengths:JavaScriptexcelsindynamictypingandasynchronousprogramming,whileJavaisrobustwithstrongOOPandtyping.1)JavaScript'sdynamicnatureallowsforrapiddevelopmentandprototyping,withasync/awaitfornon-blockingI/O.2)Java'sOOPf

JAVAACHIEVESPLATFORMINDEPENTENCETHROUGHJAVAVIRTAILMACHINE(JVM)和BYTECODE.1)THEJVMINTERPRETSBBYTECODE,允许theingthesmecodetorunonanyanyanyanyplatformwithajvm.2)

Java'splatFormIndependecemeanSapplicationsCanrunonAnyPlatFormWithAjvm,使“ Writeonce,RunanyWhere”。

JVM'SperformanceIsCompetitiveWithOtherRuntimes,operingabalanceOfspeed,安全性和生产性。1)JVMUSESJITCOMPILATIONFORDYNAMICOPTIMIZAIZATIONS.2)c提供NativePernativePerformanceButlanceButlactsjvm'ssafetyFeatures.3)

JavaachievesPlatFormIndependencEthroughTheJavavIrtualMachine(JVM),允许CodeTorunonAnyPlatFormWithAjvm.1)codeisscompiledIntobytecode,notmachine-specificodificcode.2)bytecodeisisteredbytheybytheybytheybythejvm,enablingcross-platerssectectectectectross-eenablingcrossectectectectectection.2)

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

禅工作室 13.0.1
功能强大的PHP集成开发环境

SublimeText3汉化版
中文版,非常好用