结构图
Person类
package reflect;
public class Person {
public String name;
private int id;
public Person() {
}
public Person(String name, int id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public void eat(){
System.out.println("Person类中不带参数的eat方法");
}
public void eat(String s){
System.out.println("Person类带String参数的eat方法");
}
public String eat(String s, int i){
System.out.println("Person类带String参数和int参数的eat方法");
return s;
}
private String privateEat(String s){
System.out.println("民以食为天");
return s;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", id=" + id +
'}';
}
}
Main类
package reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
test1();
System.out.println("==========================================================================");
test2();
System.out.println("==========================================================================");
test3();
System.out.println("==========================================================================");
test4();
System.out.println("==========================================================================");
test5();
System.out.println("==========================================================================");
test6();
}
public static void test1(){
try {
Class clazz = Class.forName("reflect.Person");
int modifiers = clazz.getModifiers();
System.out.println("类的修饰符为:" + modifiers);
String name = clazz.getName();
String simpleName = clazz.getSimpleName();
System.out.println("类的全名:" + name);
System.out.println("类名:" + simpleName);
Package p = clazz.getPackage();
System.out.println("类的包名:" + p.getName());
Class sclazz = clazz.getSuperclass();
System.out.println("类的父类:" + sclazz.getName());
Person person = (Person) clazz.newInstance();
System.out.println("调用无参构造方法创建对象:" + person);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
public static void test2(){
ArrayList<String> list = new ArrayList<>();
Class clayy = ArrayList.class;
Class sclayy = clayy.getSuperclass();
System.out.println("ArrayList的父类及其祖先类:");
while(sclayy != null){
System.out.println(sclayy.getName());
sclayy = sclayy.getSuperclass();
}
}
public static void test3(){
ArrayList<String> list = new ArrayList<>();
Class claxx = ArrayList.class;
Class[] interfaces = claxx.getInterfaces();
System.out.println("ArrayList实现的接口:");
for(Class c : interfaces){
System.out.println(c.getName());
}
}
public static void test4(){
try {
Class claww = Class.forName("reflect.Person");
Field name = claww.getField("name");
Field[] fields = claww.getFields();
for (Field f : fields) {
System.out.println("Person中的所有public属性:" + f.getName());
}
int modifiers = name.getModifiers();
System.out.println("name的属性修饰符:" + modifiers);
Class type = name.getType();
System.out.println("name的类型:" + type.getName());
String fname = name.getName();
System.out.println("name的类型:" + fname);
Person person = (Person) claww.newInstance();
name.set(person,"津少");
String s = (String) name.get(person);
System.out.println("person中name的值:" + s);
Field id = claww.getDeclaredField("id");
id.setAccessible(true);
id.set(person,20);
System.out.println("person对象:" + person.toString());
} catch (ClassNotFoundException | NoSuchFieldException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}
public static void test5(){
try {
Class clavv = Class.forName("reflect.Person");
Person person = (Person) clavv.newInstance();
Method m3 = clavv.getMethod("eat", String.class, int.class);
Method m4 = clavv.getDeclaredMethod("privateEat", String.class);
System.out.println("通过方法名获取到的方法:"+m3.getName()+"," + m4.getName());
int modifiers = m3.getModifiers();
System.out.println("m3的修饰符:"+modifiers);
Class type = m3.getReturnType();
Class[] parameterTypes = m3.getParameterTypes();
Class[] exceptionTypes = m3.getExceptionTypes();
String result1 = (String) m3.invoke(person, "测试public", 20);
System.out.println(result1);
m4.setAccessible(true);
String result2 = (String) m4.invoke(person,"测试private");
System.out.println(result2);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public static void test6(){
Class clauu = Person.class;
try {
Constructor con1 = clauu.getConstructor();
Person person1 = (Person) con1.newInstance();
Constructor con2 = clauu.getConstructor(String.class, int.class);
Person person2 = (Person) con2.newInstance("津少", 20);
System.out.println("输出person2对象:" + person2.toString());
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
输出结果
test1
类的修饰符为:1
类的全名:reflect.Person
类名:Person
类的包名:reflect
类的父类:java.lang.Object
调用无参构造方法创建对象:Person{name='null', id=0}
==========================================================================
test2
ArrayList的父类及其祖先类:
java.util.AbstractList
java.util.AbstractCollection
java.lang.Object
==========================================================================
test3
ArrayList实现的接口:
java.util.List
java.util.RandomAccess
java.lang.Cloneable
java.io.Serializable
==========================================================================
test4
Person中的所有public属性:name
name的属性修饰符:1
name的类型:java.lang.String
name的类型:name
person中name的值:津少
person对象:Person{name='津少', id=20}
==========================================================================
test5
通过方法名获取到的方法:eat,privateEat
m3的修饰符:1
Person类带String参数和int参数的eat方法
测试public
民以食为天
测试private
==========================================================================
test6
输出person2对象:Person{name='津少', id=20}