Method method = demo.getMethod("say", String.class);
method.invoke(demo.newInstance(), "名字");
运行结果
Hello 名字,my name is null-null
Method method = demo.getMethod("get" + "Age");
System.out.println(method.invoke(demo.newInstance()));
运行结果
0
Method method = demo.getMethod("set" + "Age", Integer.class);
Object obj = demo.newInstance();
method.invoke(obj, 20);
System.out.println(demo.getMethod("get" + "Age").invoke(obj));
运行结果
20
int[] temp = {1, 2, 3, 4, 5, 6};
Class> demo = temp.getClass().getComponentType();
System.out.println("获取数组类型->" + demo.getName());
System.out.println("数组长度->" + Array.getLength(temp));
System.out.println("数组的第一个元素->" + Array.get(temp, 0));
Array.set(temp, 0, 100);
System.out.println("修改后数组的第一个元素为->" + Array.get(temp, 0));
运行结果
获取数组类型->int
数组长度->6
数组的第一个元素->1
修改后数组的第一个元素为->100
String[] temp = {"str1", "str2", "str3", "str4", "str5", "str6"};
Class> obj = temp.getClass().getComponentType();
Object newArr=Array.newInstance(obj,20);
System.arraycopy(temp,0,newArr,0,temp.length);
for (int i = 0; i < Array.getLength(newArr); i++) {
System.out.print(Array.get(newArr, i) + " ");
}
运行结果
str1 str2 str3 str4 str5 str6 null null null null null null null null null null null null null null