First, describe the problem
Public classname () and Public classname (Object... Parameters) Is it the same function?
Check the test code.
1: public class Test
2: {
3: public Test()
4: {
5: System.out.println("No Constructor");
6: }
7: public Test(Object...keys)
8: {
9: System.out.println("Paramterized");
10: }
11: public static void main(String[] argvs)
12: throws Exception
13: {
14: Test a=new Test();
15: }
16: }
If the two constructors are the same, they cannot be compiled, so they are different. In fact, there is also a lot of difference in the function signature. Look at the calls in the main function. The call of this default function is "the default constructor of the class ".
Let's take a look at the following example:
1: public class Test
2: {
3: // public Test()
4: // {
5: // System.out.println("No Constructor");
6: // }
7: public Test(String...keys)
8: {
9: System.out.println("Paramterized");
10: }
11: public static void main(String[] argvs)
12: throws Exception
13: {
14: Test a=new Test();
15: }
16: }
The Variable Parameter Function is called directly after it is commented out. Try the legendary reflection constructor:
1: public static void main(String[] argvs)
2: throws Exception
3: {
4: java.lang.reflect.Constructor c = Test.class.getConstructor();
5: System.out.println(null==c?"Not found":"found");
6: }
Unfortunately, the error bird:
Exception in thread "Main" Java. Lang. nosuchmethodexception: COM. jeasonzhao. Report. Engine. Test. Test. <init> ()
At java. Lang. Class. getconstructor0 (class. Java: 2647)
At java. Lang. Class. getconstructor (class. Java: 1629)
At com. jeasonzhao. Report. Engine. Test. Test. Main (test. Java: 20)
That is to say, we cannot find this stuff. Therefore, it is indirectly proved that the two constructors are different, but are the following two identical?
1: public Test(String...keys)
2: {
3: System.out.println("Paramterized");
4: }
5: public Test(String[] argc)
6: {
7: System.out.println("String Array Paramterized");
8: }
In IDE, the compilation fails, and the names are duplicated. In fact, the two are the same.
Try again with some fancy features:
1: public Test(Object ...argc)
2: {
3: System.out.println("Object Array Paramterized");
4: if(null != argc)
5: {
6: int nid=0;
7: for(Object o : argc)
8: {
9: System.out.println("\t"+(nid++)+"> " + (null == o ? "[NULL]" : o.toString()));
10: }
11: }
12: }
13:
14: public static void main(String[] argvs)
15: throws Exception
16: {
17: Test t = new Test(1,"S"
18: ,new String[]{"I AM IN STRING ARRAY","STRING ARRAY END"}
19: ,new Object[]{"OBJ1","OBJ2"}
20: ,null
21: ,"END");
22: Test t2=new Test(new String[]{"I AM IN STRING ARRAY","STRING ARRAY END"});
23: Test t3=new Test(new Object[]{"OBJ1","OBJ2"});
24: }
Guess what the output is?
The key is how to handle the string [] and object [].
The result is:
Object array paramterized
0> 1
1> S
2> [ljava. Lang. String; @ 1c78e57
3> [ljava. Lang. object; @ 5224ee
4> [null]
5> end
Object array paramterized
0> I am in String Array
1> string array End
Object array paramterized
0> obj1
1> obj2
As for why, think about it yourself.
------------------------------------
Jeason Zhao
Chu Qinghou> Shen shengyi> Mr. wudou> Yu lvguang
The walking dog of a Confucius team, the logistics of a team, the CEO of a team, and the irresponsible father of the family
I know who I am, and I live silently, just like the air.