关于java中的invoke反射过程几点疑惑
巴扎黑
巴扎黑 2017-04-18 10:38:11
0
2
722
巴扎黑
巴扎黑

reply all(2)
黄舟

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.

Ty80

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....

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!