https://blog.youkuaiyun.com/stonesing/article/details/52358288
package Interview;
import java.lang.*;
import java.lang.reflect.*;
//https://blog.youkuaiyun.com/stonesing/article/details/52358288
//一个person父类
class Person {
private int age;
private String name;
public Person() {
}
public Person(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
// 一个接口
interface ActionInterface {
public void walk(int m);
}
// 超人子类实现接口
class Superman extends Person implements ActionInterface {
private boolean BlueBriefs;
public void fly() {
System.out.println("超人会飞耶~~");
}
public void setBlueBriefs(boolean blueBriefs) {
BlueBriefs = blueBriefs;
}
@Override
public void walk(int m) {
// TODO Auto-generated method stub
System.out.println("超人会走耶~~走了" + m + "米就走不动了!");
}
}
public class reflect_demo {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
demo1();
System.out.println("===============================================");
demo2();
System.out.println("===============================================");
demo3();
System.out.println("===============================================");
demo4();
System.out.println("===============================================");
demo5();
System.out.println("===============================================");
demo6();
System.out.println("===============================================");
demo7();
System.out.println("===============================================");
demo8();
}
public static void demo1() {
Person person = new Person();
System.out.println(
"Demo1: 包名: " + person.getClass().getPackage().getName() + " 完整类名:" + person.getClass().getName());
}
// 能通过反射得到类
public static void demo2() throws ClassNotFoundException {
// 定义两个类型都未知的Class , 设置初值为null, 看看如何给它们赋值成Person类
Class<?> class1 = null;
Class<?> class2 = null;
// 写法1, 可能抛出 ClassNotFoundException
// Class.forName
class1 = Class.forName("Interview.Person");
System.out.println("Demo2:(写法1) 包名: " + class1.getPackage().getName() + "," + "完整类名: " + class1.getName());
// 写法2
class2 = Person.class;
System.out.println("Demo2:(写法2) 包名: " + class2.getPackage().getName() + "," + "完整类名: " + class2.getName());
}
// 通过Java反射机制,用Class 创建类对象
public static void demo3() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
Class<?> class1 = null;
class1 = Class.forName("Interview.Person");
// 得到实例
Person person = (Person) class1.newInstance();
person.setName("aa");
person.setAge(3);
System.out.println("Demo3: " + person.getName() + " : " + person.getAge());
}
// 通过反射得到一个类的构造函数
public static void demo4() throws ClassNotFoundException, IllegalArgumentException, InstantiationException,
IllegalAccessException, InvocationTargetException {
Class<?> class1 = null;
Person person1 = null;
Person person2 = null;
class1 = Class.forName("Interview.Person");
// 得到构造函数的集合
Constructor<?>[] constructors = class1.getConstructors();
person1 = (Person) constructors[0].newInstance();
person1.setAge(30);
person1.setName("zhangsan");
person2 = (Person) constructors[1].newInstance(20, "lisi");
System.out.println("Demo4: " + person1.getName() + " : " + person1.getAge() + " , " + person2.getName()
+ " : " + person2.getAge());
}
// 通过Java反射机制操作成员变量, set 和 get
public static void demo5() throws IllegalArgumentException, IllegalAccessException, SecurityException,
NoSuchFieldException, InstantiationException, ClassNotFoundException {
Class<?> class1 = null;
class1 = Class.forName("Interview.Person");
Object obj = class1.newInstance();
Field personNameField = class1.getDeclaredField("name");
personNameField.setAccessible(true);
personNameField.set(obj, "wangwu");
System.out.println("Demo5: 修改属性之后得到属性变量的值:" + personNameField.get(obj));
}
// 通过Java反射机制得到类的一些属性: 继承的接口,父类,函数信息,成员信息,类型等
public static void demo6() throws ClassNotFoundException {
Class<?> class1 = null;
class1 = Class.forName("Interview.Superman");
// 取得父类名称
Class<?> superClass = class1.getSuperclass();
System.out.println("Demo6: SuperMan类的父类名: " + superClass.getName());
Field[] fields = class1.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
System.out.println("类中的成员:" + fields[i]);
}
// 取得类方法
Method[] methods = class1.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
System.out.println("Demo6,取得SuperMan类的方法:");
System.out.println("函数名:" + methods[i].getName());
System.out.println("函数返回类型:" + methods[i].getReturnType());
System.out.println("函数访问修饰符:" + Modifier.toString(methods[i].getModifiers()));
System.out.println("函数代码写法: " + methods[i]);
}
// 取得类实现的接口,因为接口类也属于Class,所以得到接口中的方法也是一样的方法得到哈
Class<?> interfaces[] = class1.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
System.out.println("实现的接口类名: " + interfaces[i].getName());
}
}
// Demo7: 通过Java反射机制调用类方法
public static void demo7() throws ClassNotFoundException, SecurityException, NoSuchMethodException,
IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> class1 = null;
class1 = Class.forName("Interview.Superman");
System.out.println("Demo7: \n调用无参方法fly():");
Method method = class1.getMethod("fly");
method.invoke(class1.newInstance());
System.out.println("调用有参方法walk(int m):");
method = class1.getMethod("walk", int.class);
method.invoke(class1.newInstance(), 100);
}
// Demo8: 通过Java反射机制得到类加载器信息
public static void demo8() throws ClassNotFoundException {
Class<?> class1 = null;
class1 = Class.forName("Interview.Superman");
String nameString = class1.getClassLoader().getClass().getName();
System.out.println("Demo8: 类加载器类名: " + nameString);
}
}
运行结果:
Demo1: 包名: Interview 完整类名:Interview.Person
===============================================
Demo2:(写法1) 包名: Interview,完整类名: Interview.Person
Demo2:(写法2) 包名: Interview,完整类名: Interview.Person
===============================================
Demo3: aa : 3
===============================================
Demo4: zhangsan : 30 , lisi : 20
===============================================
Demo5: 修改属性之后得到属性变量的值:wangwu
===============================================
Demo6: SuperMan类的父类名: Interview.Person
类中的成员:private boolean Interview.Superman.BlueBriefs
Demo6,取得SuperMan类的方法:
函数名:fly
函数返回类型:void
函数访问修饰符:public
函数代码写法: public void Interview.Superman.fly()
Demo6,取得SuperMan类的方法:
函数名:walk
函数返回类型:void
函数访问修饰符:public
函数代码写法: public void Interview.Superman.walk(int)
Demo6,取得SuperMan类的方法:
函数名:setBlueBriefs
函数返回类型:void
函数访问修饰符:public
函数代码写法: public void Interview.Superman.setBlueBriefs(boolean)
实现的接口类名: Interview.ActionInterface
===============================================
Demo7:
调用无参方法fly():
超人会飞耶~~
调用有参方法walk(int m):
超人会走耶~~走了100米就走不动了!
===============================================
Demo8: 类加载器类名: sun.misc.Launcher$AppClassLoader