📜  Java的无效方法重载

📅  最后修改于: 2022-05-13 01:55:35.998000             🧑  作者: Mango

Java的无效方法重载

重载方法是那些属于同一个类,具有相同名称但不同参数的方法。方法重载的概念来自多态。从字面上看,“poly”的意思是许多,而“morphism”的意思是形式。

考虑一个现实生活中的水是多晶型的例子,因为它可以有多种形式(固体、液体和气体)。同样,在Java,我们可以在同一个类中创建多个同名方法。在这里,我们将讨论Java的无效方法重载,但在此之前让我们简要回顾一下方法重载。

方法重载的条件

  • 我们可以在同一个类中创建多个同名方法。
  • 参数的数量、参数的顺序和参数的类型应该不同。

插图:方法重载

Java
// Java Program to Illustrate Method Overloading
  
// Main class
class GFG {
  
    // Method 1
    void show()
    {
        // Print statement
        System.out.println("Method to be overloaded.");
    }
  
    // Method 2
    // Overloading Method 1
    // by changing arguments
    void show(int x)
    {
        // Print statement
        System.out.println("Overloaded method:: 1");
    }
  
    // Method 3
    // Overloading show method by changing arguments
    void show(String a, int x)
    {
        System.out.println("Overloaded method:: " + x);
    }
  
    // Method 4
    // Overloading show method
    // by changing arguments
    void show(String a, int b, boolean c)
    {
        System.out.println("Overloaded method:: " + b);
    }
  
    // Method 5
    // Overloading Metohd 1 by
    // changing arguments as well as return type
    String show(String s)
    {
        // Print statement
        return "Overloaded method:: 5";
    }
  
    // Method 6
    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of class inside main()
        GFG obj = new GFG();
  
        // Calling all methofs as defined above
        // to seek overloading concepts
        obj.show();
        obj.show(1);
        obj.show("String", 2);
        obj.show("String", 3, true);
        System.out.println(obj.show("String"));
        obj.show('a');
    }
}


Java
// Demo class
class Demo {
    // Programmer defined "mymethod"
    public int myMethod(int num1, int num2)
    {
        System.out.println("First myMethod of class Demo");
        return num1 + num2;
    }
    // Trying to overload "mymethod"
    public int myMethod(int num3, int num4)
    {
        System.out.println("Second myMethod of class Demo");
        return num4 - num3;
    }
}
// Driver class
class GFG {
    // main method
    public static void main(String args[])
    {
        Demo obj1 = new Demo();
        obj1.myMethod(1, 2);
        obj1.myMethod(3, 4);
    }
}


Java
// Java Program to Illiustarte No Roleplay of Returntype
// Even changed in Method Overloading
  
// Main class
class GFG {
  
    int a, b;
  
    // Declared method
    void add(int x, int y)
    {
        // This refers to current instance itself
        this.a = x;
        this.b = y;
  
        // Printing the sum
        System.out.println("SUM:: " + (a + b));
    }
  
    // Method 2
    // To add numbers
    // Overloading the above declared method by
    // changing the return type only
    double add(int x, int y)
    {
        this.a = x;
        this.b = y;
        return a + b;
    }
  
    // Method 3
    // Main method method
    public static void main(String[] args)
    {
        // Creating object of class inside main()
        GFG obj = new GFG();
  
        // Calling add() method by passing
        // custom inputs as parameters
        obj.add(5, 2);
  
        // Trying printing the sum
        System.out.println("Sum:: " + obj.add(3, 4));
    }
}


输出-



Method to be overloaded.
Overloaded method:: 1
Overloaded method:: 2
Overloaded method:: 3
Overloaded method:: 5
Overloaded method:: 1

什么时候会出现无效方法重载的情况?

由于以下原因,会出现无效方法重载的情况:

  1. 如果我们尝试调用多个具有相同名称和参数列表的方法。这可以从代码块 1 中得到证明。
  2. 当我们尝试仅通过更改返回类型来重载方法时。这可以从代码块 2 中得到证明。

执行:

考虑下面给出的例子。当我们尝试调用“ add(1,2) ”方法时,编译器会感到困惑,因为没有这样的指令更喜欢 int 而不是 double ,反之亦然,结果,它会显示编译错误。

int add(int a, int b)
double add(int a, int b)

示例 1-A

Java

// Demo class
class Demo {
    // Programmer defined "mymethod"
    public int myMethod(int num1, int num2)
    {
        System.out.println("First myMethod of class Demo");
        return num1 + num2;
    }
    // Trying to overload "mymethod"
    public int myMethod(int num3, int num4)
    {
        System.out.println("Second myMethod of class Demo");
        return num4 - num3;
    }
}
// Driver class
class GFG {
    // main method
    public static void main(String args[])
    {
        Demo obj1 = new Demo();
        obj1.myMethod(1, 2);
        obj1.myMethod(3, 4);
    }
}

输出:

prog.java:7: error: method myMethod(int,int) is already defined in class Demo
    public int myMethod(int num3, int num4)
               ^
1 error

示例 1-B

Java

// Java Program to Illiustarte No Roleplay of Returntype
// Even changed in Method Overloading
  
// Main class
class GFG {
  
    int a, b;
  
    // Declared method
    void add(int x, int y)
    {
        // This refers to current instance itself
        this.a = x;
        this.b = y;
  
        // Printing the sum
        System.out.println("SUM:: " + (a + b));
    }
  
    // Method 2
    // To add numbers
    // Overloading the above declared method by
    // changing the return type only
    double add(int x, int y)
    {
        this.a = x;
        this.b = y;
        return a + b;
    }
  
    // Method 3
    // Main method method
    public static void main(String[] args)
    {
        // Creating object of class inside main()
        GFG obj = new GFG();
  
        // Calling add() method by passing
        // custom inputs as parameters
        obj.add(5, 2);
  
        // Trying printing the sum
        System.out.println("Sum:: " + obj.add(3, 4));
    }
}

输出:

prog.java:8: error: method add(int,int) is already defined in class GFG
  double add(int x,int y){
         ^
prog.java:17: error: 'void' type not allowed here
      System.out.println("Sum:: "+obj.add(3,4));
                                         ^
2 errors