java抽象類,java方法重載和重載方法_Java中的方法重載

 2023-11-19 阅读 30 评论 0

摘要: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 c

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) {};
}

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中是允許的。

允許方法重載的實例 (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概念,其中將較小等級的數據類型提升為較高等級的數據類型。
下圖說明了數據類型大小提升順序:

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.

注意 :如果找不到匹配的數據類型,則將通過的值數據類型自動提升為下一行(請參見上圖)。

java方法重載?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}}

    java構造方法。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)

java框架。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){}

    java final,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中的方法重載

    編譯時錯誤,因為參數列表相同。 即使方法的返回類型和變量名稱不同,也不是有效的情況。 由于返回類型或變量名稱文字不是方法重載的條件。

java方法,That’s all for method overloading in java.

這就是Java中方法重載的全部。

Reference: Wikipedia

參考: 維基百科

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

java方法重載和重載方法

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/183191.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息