Home  >  Q&A  >  body text

关于java中的invoke反射过程几点疑惑

巴扎黑巴扎黑2762 days ago773

reply all(2)I'll reply

  • 黄舟

    黄舟2017-04-18 10:40:11

    1. This is a generic Class<?> c = A.class ? It can be any type, such as ArrayList array = new ArrayList(); array.add(1); or array.add("1"); Yes, but the data type you put in this ArrayList is obviously messed up. Such data is unsafe and has no value, so it must be as follows ArrayList<String> array=new ArrayList<String>(); At this time, your array.add(1); An error will be reported and int type data cannot be stored. 2. Compilation does not report an error but a warning. Warn error is different. 3. After modifying the parameters as shown in the figure, there will be no warning.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:40:11

    In fact, just write it like this.

    public static void main(String[] args) {
            A a1 = new A();
            Class<?> c = A.class;
            try {
                Method m = c.getMethod("print", int.class, int.class);
                Object o = m.invoke(a1, 10, 20);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    Why passInteger[]会有警告,因为参数类型是Object....

    reply
    0
  • Cancelreply