重载允许不同的方法具有相同的名称,但具有不同的签名,其中签名可能因输入参数的数量或输入参数的类型或两者而不同。重载与编译时(或静态)多态性有关。
// Java program to demonstrate working of method // overloading in Java. public class Sum { // Overloaded sum(). This sum takes two int parameters public int sum( int x, int y) { return (x + y); } // Overloaded sum(). This sum takes three int parameters public int sum( int x, int y, int z) { return (x + y + z); } // Overloaded sum(). This sum takes two double parameters public double sum( double x, double y) { return (x + y); } // Driver code public static void main(String args[]) { Sum s = new Sum(); System.out.println(s.sum( 10 , 20 )); System.out.println(s.sum( 10 , 20 , 30 )); System.out.println(s.sum( 10.5 , 20.5 )); } } |
输出:
30 60 31.0
问题出现了: 问:如果确切的原型与参数不匹配怎么办。 答案。 在优先级方面,请执行以下步骤:
- 类型转换,但在同一系列中转换为更高的类型(范围)。
- 类型转换到下一个更高的系列(假设int数据类型没有长数据类型可用,那么它将搜索float数据类型)。
让我们举一个例子来澄清这个概念:-
class Demo { public void show( int x) { System.out.println( "In int" + x); } public void show(String s) { System.out.println( "In String" + s); } public void show( byte b) { System.out.println( "In byte" + b); } } class UseDemo { public static void main(String[] args) { byte a = 25 ; Demo obj = new Demo(); obj.show(a); // it will go to // byte argument obj.show( "hello" ); // String obj.show( 250 ); // Int obj.show( 'A' ); // Since char is // not available, so the datatype // higher than char in terms of // range is int. obj.show( "A" ); // String obj.show( 7.5 ); // since float datatype // is not available and so it's higher // datatype, so at this step their // will be an error. } } |
优势是什么? 我们不必为做同一件事的函数创建和记住不同的名称。例如,在我们的代码中,如果Java不支持重载,我们必须创建方法名,比如sum1、sum2……或sum2Int、sum3Int……等等。
我们能在返回类型上重载方法吗? 我们 不能 按返回类型重载。这种行为在C++中是相同的。有关详细信息,请参阅此
public class Main { public int foo() { return 10 ; } // compiler error: foo() is already defined public char foo() { return 'a' ; } public static void main(String args[]) { } } |
然而,在显式指定被调用函数的数据类型的情况下,可以对返回类型使用重载方法。请看下面的例子:
// Java program to demonstrate the working of method // overloading in static methods public class Main { public static int foo( int a) { return 10 ; } public static char foo( int a, int b) { return 'a' ; } public static void main(String args[]) { System.out.println(foo( 1 )); System.out.println(foo( 1 , 2 )); } } |
输出:
10 a
// Java program to demonstrate working of method // overloading in methods class A { public int foo( int a) { return 10 ; } public char foo( int a, int b) { return 'a' ; } } public class Main { public static void main(String args[]) { A a = new A(); System.out.println(a.foo( 1 )); System.out.println(a.foo( 1 , 2 )); } } |
输出:
10 a
我们能重载静态方法吗? 答案是 对 ’. 我们可以有两个或更多的静态方法,名称相同,但输入参数不同。例如,考虑下面的java程序。参考 这 详细信息。
我们能重载只因静态关键字不同的方法吗? 我们 不能 如果Java中的两个方法仅因static关键字不同(参数数量和参数类型相同),则重载它们。参见下面的Java程序示例。参考 这 详细信息。
我们能在Java中重载main()吗? 像其他静态方法一样,我们 可以 Java中的重载main() 。有关详细信息,请参阅Java中的重载main()。
// A Java program with overloaded main() import java.io.*; public class Test { // Normal main() public static void main(String[] args) { System.out.println( "Hi Geek (from main)" ); Test.main( "Geek" ); } // Overloaded main methods public static void main(String arg1) { System.out.println( "Hi, " + arg1); Test.main( "Dear Geek" , "My Geek" ); } public static void main(String arg1, String arg2) { System.out.println( "Hi, " + arg1 + ", " + arg2); } } |
输出:
Hi Geek (from main) Hi, Geek Hi, Dear Geek, My Geek
Java支持操作符重载吗? 与C++不同,java不允许用户定义的重载操作符。在内部Java重载运算符,例如,+重载用于连接。
重载和重载的区别是什么 最重要的 ?
相关文章:
本文由 Shubham Agrawal .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。