java方法重载和重载方法_Java中的方法重载

java方法重载和重载方法

The concept of Method Overloading in Java is where a class can have multiple methods with the same name, provided that their argument constructions are different. Method Overloading is comparable to constructor overloading where we can implement multiple constructors (also the same name), provided that these constructors have different argument construction.

Java中方法重载的概念是,一个类可以有多个具有相同名称的方法,只要它们的参数构造不同即可。 方法重载类似于构造函数重载,在这里我们可以实现多个构造函数(也具有相同的名称),只要这些构造函数具有不同的参数构造即可。

Java中的方法重载 (Method Overloading in Java)

  • Method name should be exactly same. Java is case sensitive, so two methods with name foo()
    and fOO() are totally different and doesn’t come under method overloading in java.

    方法名称应完全相同。 Java区分大小写,因此两个名称为foo()
    fOO()完全不同,并且不会在Java中发生方法重载。
  • The difference between overloaded methods are the arguments. Overloaded methods should have different method arguments. For example, different number of parameters, different data types of parameters etc.

    重载方法之间的区别是参数。 重载的方法应具有不同的方法参数。 例如,不同数量的参数,不同参数的数据类型等。
  • Method return type have no significance for overloading, java compiler will complain about duplicate method in the class.

    方法返回类型对于重载没有意义,java编译器会抱怨类中的重复方法。
  • Method parameter names have no significance in method overloading. So in terms of java, foo(int index) and foo(int i) are same.

    方法参数名称在方法重载中没有意义。 因此就Java而言, foo(int index)foo(int i)是相同的。
  • Method overloading is also called compile time polymorphism. Read more at OOPS Concepts in Java.

    方法重载也称为编译时多态。 在Java的OOPS Concepts中阅读更多内容。

Let’s look into some examples of method overloading in java.

让我们研究一下Java中方法重载的一些示例。

Java示例中的方法重载 (Method Overloading in Java Example)

package com.journaldev.methodoverloading;

public class GetAverage {
	private static void gatherInput(int dataOne, int dataTwo) {
	};

	private static void gatherInput(int dataThree, String dataFour, float dataFive) {
	};
}

The snippet above shows a classic example of Method Overloading. Two gatherInput() methods are declared under GetAverage Class. In usual cases, java compiler would display the error: Duplicate method gatherInput() in type GetAverage. However, in this case, we see that the two methods possess different argument list, which makes this approach in java permissible.

上面的代码段显示了方法重载的经典示例。 在GetAverage类下声明了两个gatherInput()方法。 在通常情况下,java编译器会显示错误: Duplicate method gatherInput() in type GetAverage 。 但是,在这种情况下,我们看到这两个方法具有不同的参数列表,这使得该方法在Java中是允许的。

允许方法重载的实例 (Instances where Method Overloading is permissible)

  1. Parameter Quantity: Method Overloading is allowed within the class given that the number of parameters of the overloaded methods are not the same.
    package com.journaldev.methodoverloading;
    
    public class GetAverage {
    	private static void gatherInput(int dataOne, int dataTwo) {
    	};
    
    	private static void gatherInput(int dataOne, int dataTwo, int dataThree) {
    	};
    }

    参数数量 :如果重载方法的参数数量不同,则允许在类内重载方法。
  2. Parameter Data Types: Method Overloading is allowed within the class given that at least one pair of parameters, from each overloaded method, are of different data type.
    public class GetAverage {
    	private static void gatherInput(int dataOne, int dataTwo) {
    	};
    
    	private static void gatherInput(int dataOne, double dataTwo) {
    	};
    }

    参数数据类型 :如果每个重载方法中的至少一对参数具有不同的数据类型,则在类内允许方法重载。
  3. Order of Parameters: Method Overloading is allowed within the class given that the order of the data type variables are not the same for overloaded methods.
    public class GetAverage {
    	private static void gatherInput(int dataOne, double dataTwo) {
    	};
    
    	private static void gatherInput(double dataOne, int dataTwo) {
    	};
    }

    参数顺序 :如果重载方法的数据类型变量的顺序不同,则在类内允许方法重载。

不允许方法重载的实例 (Instance where Method Overloading is Impermissible)

  • Method Return Type: Method Overloading is prohibited within the class with methods having identical parameter list but with different method return type. Bear in mind that Overloading focuses on the parameters, not the return type.
    public class GetAverage {
    	private static int gatherInput(int dataOne, double dataTwo) {
    		return 0;
    	};
    
    	private static double gatherInput(int dataOne, double dataTwo) {
    		return 0;
    	};
    }

    Above code will produce compiler error: Duplicate method gatherInput(int, double) in type GetAverage.

    方法返回类型 :禁止在类中使用具有相同参数列表但具有不同方法返回类型的方法的方法重载。 请记住,重载侧重于参数,而不是返回类型。

    上面的代码将产生编译器错误: Duplicate method gatherInput(int, double) in type GetAverage

Java中的类型提升 (Type Promotion in Java)

Type Promotion is a java concept where a data type of smaller rank is promoted to the data type of higher rank.
The figure below illustrates the data type size promotion order:

类型提升是一个Java概念,其中将较小等级的数据类型提升为较高等级的数据类型。
下图说明了数据类型大小提升顺序:

As can be observed from the above diagram, byte may be promoted to short. Int data type have three possible promotions: double, long, or float.

从上图可以看出,字节可以提升为短。 Int数据类型具有三种可能的提升:double,long或float。

Java中的类型提升和方法重载 (Type Promotion and Method Overloading in Java)

Type Promotion goes hand in hand with Method Overloading in Java. Type Promotion extends the logic of method overloading when it comes to deciding which overloaded method is used, in cases where values passed does not coincide with any of the parameter data types established.

类型提升与Java中的方法重载齐头并进。 在传递的值与已建立的任何参数数据类型不一致的情况下,类型升级扩展了方法重载的逻辑,以决定使用哪种重载方法。

Note: Passed value data type is automatically promoted to its data type next in line (refer to figure above), if no matching data type is found.

注意 :如果找不到匹配的数据类型,则将通过的值数据类型自动提升为下一行(请参见上图)。

Let’s look into some of the examples of type promotion and overloading in java.

让我们看一下Java中类型提升和重载的一些示例。

  1. Type Promotion Example 1:
    package com.journaldev.methodoverloading;
    
    public class TypePromotionExample {
    
    	void add(float dataOne, float dataTwo) {
    		System.out.println(dataOne + dataTwo + " type float");
    	}
    
    	void add(long dataOne, long dataTwo) {
    		System.out.println(dataOne + dataTwo + " type long");
    	}
    
    	public static void main(String[] args) {
    		TypePromotionExample tpe = new TypePromotionExample();
    		tpe.add(5, 5); // integer values are passed
    	}
    
    }

    Output: 10 type long

    Two integers are passed in overloaded add() method. Since no overload methods possess (int, int) arguments, the passed values will be promoted to the nearest bigger type i.e. long.

    类型提升示例1

    输出 :10型长

    在重载的add()方法中传递了两个整数。 由于没有重载方法拥有(int,int)参数,因此传递的值将提升为最接近的较大类型,即long。

  2. Type Promotion Example 2:
    package com.journaldev.methodoverloading;
    
    public class TypePromotionExample {
    
    	void add(float dataOne, float dataTwo){
    		System.out.println(dataOne+dataTwo + " type float");
    	}
    	
    	void add(double dataOne, double dataTwo){
    		System.out.println(dataOne+dataTwo + " type long");
    	}
    
    	public static void main(String[] args) {
    		TypePromotionExample tpe = new TypePromotionExample();
    		tpe.add(5, 5); // integer values are passed
    	}
    
    }

    Output: 10.0 type float

    Here the nearest bigger type for int is float, hence the output.

    类型提升示例2

    输出 :10.0型浮子

    这里最接近int的较大类型是float,因此是输出。

  3. Type Promotion Example 3:
    package com.journaldev.methodoverloading;
    
    public class TypePromotionExample {
    
    	void add(double dataOne, int dataTwo){
    		System.out.println("1st method invoked.");
    	}
    	
    	void add(long dataOne, long dataTwo){
    		System.out.println("2nd method invoked.");
    	}
    
    	public static void main(String[] args) {
    		TypePromotionExample tpe = new TypePromotionExample();
    		tpe.add(5, 5); // integer values are passed
    	}
    
    }

    Here java compiler will produce error as The method add(double, int) is ambiguous for the type TypePromotionExample. It’s because both methods are applicable and type conversion is required for any one of them to execute, so java compiler can’t decide which one to go for.

    类型提升示例3

    在这里,java编译器将产生错误,因为The method add(double, int) is ambiguous for the type TypePromotionExample 。 这是因为这两种方法均适用,并且任何一种方法都必须执行类型转换,所以Java编译器无法确定要使用哪种方法。

Java有效/无效情况下的方法重载 (Method Overloading in Java Valid/Invalid Cases)

Let’s look at some code snippets for method overloading use case, whether they are valid or not.

让我们看一些方法重载用例的代码片段,无论它们是否有效。

  • Method Overloading in Java Example 1
    void method(int dataOne, int dataTwo, int dataThree){}
    
    void method(int dataFour, int dataFive, int dataSix){}

    Compile time error message Duplicate method method(int, int, int). Even if the variable names differ, java only looks into data types being created and in this example, are both identical(int, int, int).

    Java示例中的方法重载

    编译时错误消息Duplicate method method(int, int, int) 。 即使变量名称不同,java也会仅查看正在创建的数据类型,并且在此示例中,它们都是相同的(int,int,int)。

  • Method Overloading in Java Example 2
    void method(int dataOne, int dataTwo){}
    
    void method(float dataThree, float dataFour){}

    Valid case of method overloading. Data types of arguments are different.

    Java示例2中的方法重载

    方法重载的有效情况。 参数的数据类型不同。

  • Method Overloading in Java Example 3
    void method(int dataOne, int dataTwo){}
    
    void method(int dataThree){}

    Valid case of method overloading, number of arguments are different.

    Java示例3中的方法重载

    方法重载的有效情况,参数数量不同。

  • Method Overloading in Java Example 4
    void method(int dataOne, float dataTwo){}
    
    void method(float dataThree, int dataFour){}

    Valid case of method overloading. Sequence of the data types of parameters are different, first method having (int, float) and second having (float, int).

    Java示例4中的方法重载

    方法重载的有效情况。 参数的数据类型顺序不同,第一种方法具有(int,float),第二种方法具有(float,int)。

  • Method Overloading in Java Example 5
    int method(int dataOne, float dataTwo){ return 0;}
    	
    float method(int dataThree, float dataFour){ return 0.0;}

    Compile time error because argument lists are identical. Even if return type of methods and variable names are different, it’s not a valid case. Since return type or variable name literals are not criteria for method overloading.

    Java示例5中的方法重载

    编译时错误,因为参数列表相同。 即使方法的返回类型和变量名称不同,也不是有效的情况。 由于返回类型或变量名称文字不是方法重载的条件。

That’s all for method overloading in java.

这就是Java中方法重载的全部。

Reference: Wikipedia

参考: 维基百科

翻译自: https://www.journaldev.com/16807/method-overloading-in-java

java方法重载和重载方法

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值