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中方法重載的概念是,一個類可以有多個具有相同名稱的方法,只要它們的參數構造不同即可。 方法重載類似于構造函數重載,在這里我們可以實現多個構造函數(也具有相同的名稱),只要這些構造函數具有不同的參數構造即可。
foo()
fOO()
are totally different and doesn’t come under method overloading in java. 方法名稱應完全相同。 Java區分大小寫,因此兩個名稱為foo()
fOO()
完全不同,并且不會在Java中發生方法重載。 foo(int index)
and foo(int i)
are same. 方法參數名稱在方法重載中沒有意義。 因此就Java而言, foo(int index)
和foo(int i)
是相同的。 Let’s look into some examples of method overloading in java.
讓我們研究一下Java中方法重載的一些示例。
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) {};
}
java抽象類。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中是允許的。
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) {};
}
參數數量 :如果重載方法的參數數量不同,則允許在類內重載方法。 public class GetAverage {private static void gatherInput(int dataOne, int dataTwo) {};private static void gatherInput(int dataOne, double dataTwo) {};
}
參數數據類型 :如果每個重載方法中的至少一對參數具有不同的數據類型,則在類內允許方法重載。 public class GetAverage {private static void gatherInput(int dataOne, double dataTwo) {};private static void gatherInput(double dataOne, int dataTwo) {};
}
參數順序 :如果重載方法的數據類型變量的順序不同,則在類內允許方法重載。 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
。
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概念,其中將較小等級的數據類型提升為較高等級的數據類型。
下圖說明了數據類型大小提升順序:
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。
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.
注意 :如果找不到匹配的數據類型,則將通過的值數據類型自動提升為下一行(請參見上圖)。
java方法重載?Let’s look into some of the examples of type promotion and overloading in java.
讓我們看一下Java中類型提升和重載的一些示例。
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。
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}}
java構造方法。Output: 10.0 type float
Here the nearest bigger type for int is float, hence the output.
類型提升示例2 :輸出 :10.0型浮子
這里最接近int的較大類型是float,因此是輸出。
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.
在這里,java編譯器將產生錯誤,因為The method add(double, int) is ambiguous for the type TypePromotionExample
。 這是因為這兩種方法均適用,并且任何一種方法都必須執行類型轉換,所以Java編譯器無法確定要使用哪種方法。
java框架。Let’s look at some code snippets for method overloading use case, whether they are valid or not.
讓我們看一些方法重載用例的代碼片段,無論它們是否有效。
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).
編譯時錯誤消息Duplicate method method(int, int, int)
。 即使變量名稱不同,java也會僅查看正在創建的數據類型,并且在此示例中,它們都是相同的(int,int,int)。
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中的方法重載方法重載的有效情況。 參數的數據類型不同。
void method(int dataOne, int dataTwo){}void method(int dataThree){}
java final,Valid case of method overloading, number of arguments are different.
Java示例3中的方法重載方法重載的有效情況,參數數量不同。
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)。
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中的方法重載編譯時錯誤,因為參數列表相同。 即使方法的返回類型和變量名稱不同,也不是有效的情況。 由于返回類型或變量名稱文字不是方法重載的條件。
java方法,That’s all for method overloading in java.
這就是Java中方法重載的全部。
Reference: Wikipedia
參考: 維基百科
翻譯自: https://www.journaldev.com/16807/method-overloading-in-java
java方法重載和重載方法
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态