package mypackage; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; How to fix my code below?
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
public class testReflection {
public class You {
public You(String s) {
}
public void f(String s, int i) {
System.out.println(i + 100);
}
}
public static void main(String[] args) throws NoSuchMethodException {
Constructor constructor =
You.class.getConstructor(testReflection.class, String.class);
try {
You y = (You)constructor.newInstance("xzy");//Exception!!!!
System.out.println("ok");
y.f("xyz",2);
}catch(Exception e){
e.printStackTrace();
}
}
}
The exception message is:
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at mypackage.testReflection.main
Aucun commentaire:
Enregistrer un commentaire