java方法重载和重载方法
Method overloading is a concept that allows to declare multiple methods with same name but different parameters in the same class.
是一个概念 ,它允许在同一类中声明具有相同名称但参数不同的多个方法。
Java supports method overloading and always occur in the same class(unlike method overriding).
Java支持方法重载,并且总是在同一个类中发生(与方法重写不同)。
Method overloading is one of the ways through which java supports polymorphism. Polymorphism is a concept of object oriented programming that deal with multiple forms. We will cover polymorphism in separate topics later on.
方法重载是Java支持多态性的方法之一。 多态是一个面向对象的编程概念,涉及多种形式。 稍后我们将在单独的主题中介绍多态。
Method overloading can be done by changing number of arguments or by changing the data type of arguments.
更改参数数量或更改 参数 的数据类型来完成
If two or more method have same name and same parameter list but differs in return type
但返回类型不同,则不能重载。
Note:
注意:重载的方法可以具有不同的访问修饰符,并且在方法重载中没有任何意义。
(There are two different ways of method overloading.)
- Different datatype of arguments
参数的不同数据类型 - Different number of arguments
参数数量不同
(Method overloading by changing data type of arguments.)
Example:
例:
In this example, we have two sum() methods that take integer and float type arguments respectively.
在此示例中,我们有两个sum()方法分别采用整数和浮点型参数。
Notice that in the same class we have two methods with same name but different types of parameters
名称相同但参数类型不同的方法
class Calculate
{
void sum (int a, int b)
{
System.out.println("sum is"+(a+b)) ;
}
void sum (float a, float b)
{
System.out.println("sum is"+(a+b));
}
Public static void main (String[] args)
{
Calculate cal = new Calculate();
cal.sum (8,5);//sum(int a, int b) is method is called.
cal.sum (4.6f, 3.8f); //sum(float a, float b) is called.
}
}
Sum is 13 Sum is 8.4
总和是13总和是8.4
You can see that sum() method is overloaded two times. The first takes two integer arguments, the second takes two float arguments.
您可以看到sum()方法被重载了两次。 第一个带有两个整数参数,第二个带有两个浮点参数。
(Method overloading by changing no. of argument.)
Example:
范例
In this example, we have two methods
在这个例子中,我们有两种方法
class Demo
{
void multiply(int l, int b)
{
System.out.println("Result is"+(l*b)) ;
}
void multiply(int l, int b,int h)
{
System.out.println("Result is"+(l*b*h));
}
public static void main(String[] args)
{
Demo ar = new Demo();
ar.multiply(8,5); //multiply(int l, int b) is method is called
ar.multiply(4,6,2); //multiply(int l, int b,int h) is called
}
}
Result is 40 Result is 48
结果是40结果是48
In this example the multiply()
method is overloaded twice. The first method takes two arguments and the second method takes three arguments.
在这个例子中, multiply()
方法被重载了两次。 第一个方法带有两个参数,第二个方法带有三个参数。
When an overloaded method is called Java look for match between the arguments to call the method and the its parameters. This match need not always be exact, sometime when exact match is not found, Java automatic type conversion plays a vital role.
调用Java的重载方法时,请在调用该方法的参数与其参数之间进行匹配。 这种匹配不一定总是精确的,有时在找不到精确匹配时,Java自动类型转换起着至关重要的作用。
(Example of Method overloading with type promotion.)
Java supports automatic type promotion, like int to long or float to double etc. In this example we are doing same and calling a function that takes one integer and second long type
一个整数和第二个long类型参数的函数。 在调用时,我们传递了整数值,Java将第二个参数视为长型。 请参见以下示例。
class Demo
{
void sum(int l,long b)
{
System.out.println("Sum is"+(l+b)) ;
}
void sum(int l, int b, int h)
{
System.out.println("Sum is"+(l+b+h));
}
public static void main (String[] args)
{
Demo ar = new Demo();
ar.sum(8,5);
}
}
Sum is 13
总和是13
(Method overloading if the order of parameters is changed)
We can have two methods with same name and parameters but the order of parameters is different.
我们可以有两个具有相同名称和参数的方法,但是参数的顺序不同。
Example:
例:
In this scenario, method overloading works but internally JVM treat it as method having different type of arguments
在这种情况下,方法重载是有效的,但是在内部JVM中,它被视为具有不同类型参数的方法
class Demo{
public void get(String name, int id)
{
System.out.println("Company Name :"+ name);
System.out.println("Company Id :"+ id);
}
public void get(int id, String name)
{
System.out.println("Company Id :"+ id);
System.out.println("Company Name :"+ name);
}
}
class MethodDemo8{
public static void main (String[] args) {
Demo obj = new Demo();
obj.get("Cherry", 1);
obj.get("Jhon", 2);
}
}
(Overloading main Method)
In Java, we can overload the main() method using different number and types of parameter but the JVM only understand the original main() method.
在Java中,我们可以使用不同数量和类型的参数来重载main()方法,但是JVM仅了解原始的main()方法。
Example:
例:
In this example, we created three main() methods having different parameter types.
在此示例中,我们创建了三个具有不同参数类型的main()方法。
public class MethodDemo10{
public static void main(intargs)
{
System.out.println("Main Method with int argument Executing");
System.out.println(args);
}
public static void main(char args)
{
System.out.println("Main Method with char argument Executing");
System.out.println(args);
}
public static void main(Double[] args)
{
System.out.println("Main Method with Double Executing");
System.out.println(args);
}
public static void main(String[] args)
{
System.out.println("Original main Executing");
MethodDemo10.main(12);
MethodDemo10.main('c');
MethodDemo10.main(1236);
}
}
(Method overloading and null error)
This is a general issue when working with objects, if same name methods having reference type parameters then be careful while passing arguments.
当使用对象时,这是一个普遍的问题,如果具有引用类型参数的同名方法在传递参数时要小心。
Below is an example in which you will know how a null value can cause an error when methods are overloaded.
下面是一个示例,您将了解在方法重载时null值如何导致错误的情况。
Example:
例:
Null value is a default value for all the reference types. It created ambiguity to JVM that reports error.
空值是所有引用类型的默认值。 它给报告错误的JVM造成了歧义。
public class Demo
{
public void test(Integer i)
{
System.out.println("test(Integer ) ");
}
public void test(String name)
{
System.out.println("test(String ) ");
}
public static void main(String [] args)
{
Demo obj = new Demo();
obj.test(null);
}
}
The method test(Integer) is ambiguous for the type Demo
test类型的test(Integer)模棱两可
原因: (Reason:)
The main reason for getting the compile time error in the above example is that here we have Integer and String as arguments which are not primitive data types in java and this type of argument do not accept any null value. When the null value is passed the compiler gets confused which method is to be selected as both the methods in the above example are accepting null.
在上面的示例中出现编译时错误的主要原因是,这里我们将Integer和String作为参数,它们不是java中的原始数据类型,并且这种类型的参数不接受任何null值。 当传递null值时,编译器会感到困惑,因为上面示例中的两个方法都接受null,因此该选择哪种方法。
However, we can solve this to pass specific reference type rather than value.
解决此问题以传递特定的引用类型而不是值。
Example:
例:
In this example, we are passing specific type argument rather than null value.
在此示例中,我们传递特定的类型参数而不是null值。
public class MethodDemo9
{
public void test(Integer i)
{
System.out.println("Method ==> test(Integer)");
}
public void test(String name)
{
System.out.println("Method ==> test(String) ");
}
public static void main(String [] args)
{
MethodDemo9 obj = new MethodDemo9 ();
Integer a = null;
obj.test(a);
String b = null;
obj.test(b);
}
}
翻译自: https://www.studytonight.com/java/method-and-overloaded-method.php
java方法重载和重载方法