The variable length parameter in Java can be expressed as follows:
String... ARGs
Its polymorphism with the string S (single parameter) method has become a very interesting problem.
Package CN. xujin; public class regex2 {public static void main (string ARGs []) {base A = new child ();. test ("one"); // base //. test ("22", "123"); // error, compilation error: Child B = (child) A; B. test ("one"); // baseb. test ("one", "two"); // child} class base {public void test (string s) {system. out. println ("base") ;}} class Child extends base {public void test (string... ARGs) {system. out. println ("child ");}}
Run the above program to know:
A is a child type, but is referenced by the base. Call. test ("one") calls the base class, while. test ("one", "two"); cannot be compiled because the base class does not have the test method of two parameters.
B is also a child type, referenced by the Child type, B. test ("one") calls the base class, while B. test ("one", "two"); call the variable length parameter method in the Child class.
The base class has only one single-parameter method, while the Child class has a single-parameter method and a variable-length parameter method. When a single-parameter method is called, a conflict occurs. In this case, the virtual opportunity directly calls the exactly-matched method regardless of the variable length parameter method.
We can see that the two or more Parameters match the overload function, while the single parameter does not conform to the override rule. It can be said that this is a special overload.
Therefore:
When two or more parameters are used, they are loaded according to the overload. When a single parameter is used,. test ("one"), base only has a single parameter method, so you must call the test method in base and output "base ". Call B. test ("one") and child have variable parameter methods and inherited Single-parameter methods. Therefore, the virtual machine directly calls the exactly-matched method regardless of the variable length parameter method, output "base ".
Let's look at a situation to verify the above points:
package cn.xujin;import java.util.regex.*;public class Regex2{public static void main(String args[]){Base a = new Child();a.test("one");//Basea.test("one","two");//BaseChild b = (Child)a;b.test("one","two");//Baseb.test("one");//Child}}class Base{public void test(String...args){System.out.println("Base");}}class Child extends Base{public void test(String s){System.out.println("Child");}}
Similarly, the base class has only one variable parameter method, while the Child class has a single-parameter method and a variable-length parameter method.
When a single parameter is used, A. Test ("one") is called, and base only has variable parameter methods. Therefore, the test method in base must be called to output "base ". Call B. test ("one") and child have variable parameter methods and inherited Single-parameter methods. Therefore, the virtual machine directly calls the exactly-matched method regardless of the variable length parameter method, output "child ".
Today I have encountered another situation, not the relationship of the parent class subclass:
package cn.xujin;import java.util.regex.*;public class A{public String doit(int x, int y){return "a";}public String doit(int...vals){return "b";}public static void main(String...args){A a = new A();System.out.println(a.doit(3, 4));//a}}
In this case, the virtual opportunity first sets aside the variable length parameter method and finds whether the method can be directly matched. If the method is found and then run, otherwise the variable length parameter method will be searched.