public
class
ReflectDemo {
public
static
void
main(String[] args)
throws
Exception {
ArrayList
<String> list =
new
ArrayList<String>
();
list.add(
"enen"
);
for
(String str:list) {
System.out.println(str);
Class
<?
extends
ArrayList> clas=
list.getClass();
ArrayList
<String> li=(ArrayList<String>
) clas.newInstance();
li.add(
"san"
);
for
(String str:li) {
System.out.println(str);
Method[] metho
=
clas.getMethods();
for
(Method met:metho) {
System.out.println(met);
Method meth
=clas.getMethod("add",Object.
class
);
//
后边的.class 是add方法的参数的类型类
meth.invoke(li, "123"
);
for
(String str:li) {
System.out.println(str);
Class
<?> c=Class.forName("com.swift.ADemo"
);
ADemo a
=
(ADemo) c.newInstance();
a.fun();
Class cl
=
a.getClass();
ADemo b
=
(ADemo) cl.newInstance();
b.fun();
Method met
=cl.getMethod("fun"
);
met.invoke(b);
Method[] m
=
c.getDeclaredMethods();
for
(Method me:m) {
System.out.println(me);
Method method
=c.getMethod("fun"
);
method.invoke(a);
class
ADemo{
public
void
fun() {
System.out.println(
"A demo."
);