本帖最后由 a12366456 于 2015-7-22 00:30 编辑
- class Person {
- private int id;
- public String name;
- public void printInfo() {
- System.out.println("name=" + name + " id=" + id);
- }
- public String subName(String name) {
- return name;
- }
- @SuppressWarnings("unused")
- private void addFri(int id) {
- System.out.println("添加" + id + "为朋友");
- }
- public Person() {
- };
- public Person(int id, String name) {
- this.id = id;
- this.name = name;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- }
- public class Fanshe_zonghe {
- public Fanshe_zonghe() {
- // TODO Auto-generated constructor stub
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- try {
- Class<?> clazz = Class.forName("com.drz.fanshe.Person");
- Field[] fields = clazz.getFields();
- // Object obj=clazz.newInstance();
- Constructor<?> constructor = clazz.getConstructor(int.class, java.lang.String.class);
- Object obj = constructor.newInstance(new Integer(234), "lihua");
- // Object obj = clazz.newInstance();
- for (Field field : fields) {
- Object o = field.get(obj);
- System.out.println(o);
- }
- Field fieldPri = clazz.getDeclaredField("id");
- fieldPri.setAccessible(true);
- Object oPri = fieldPri.get(obj);
- System.out.println(oPri);
- //
- Method methodP=clazz.getMethod("printInfo", new Class[]{});
- //methodP.invoke(obj, null); 这样会出现警告
- methodP.invoke(obj, new Object[]{});
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
复制代码
用反射拿到成员方法和使用成员方法时,参数列表为null怎么会报这个警告:The argument of type null should explicitly be cast to Object[] for the invocation of the varargs method invoke(Object, Object...) from type Method. It could alternatively be cast to Object for a varargs invocation
|
|