package com.charles.reflectDemo;
public interface myInterface1
{
public void myInterface1Method();
}
package com.charles.reflectDemo;
public interface myInterface2
{
public void myInterface2Method();
}
package com.charles.reflectDemo;
public class reflectDemo implements myInterface1,myInterface2{
private String name;
private int id;
public String desc;
public reflectDemo(String name, int id, String desc) {
super();
this.name = name;
this.id = id;
this.desc = desc;
}
public reflectDemo(String name, int id) {
super();
this.name = name;
this.id = id;
}
public reflectDemo() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public void myInterface2Method() {
System.out.println("method myInterface2Method");
}
@Override
public void myInterface1Method() {
System.out.println("method myInterface1Method");
}
private void sayHello()
{
System.out.println("method sayHello");
}
}
public static void demo1() //测试三种反射入口
{
Class> demoClazz=null;
try {
demoClazz = Class.forName("com.charles.reflectDemo.reflectDemo");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(demoClazz);
System.out.println(reflectDemo.class);
System.out.println(new reflectDemo().getClass());
}
public static void demo2() //获取所有公共方法,包括接口的和继承的,默认继承Object
{
Class> demoClazz=null;
try {
demoClazz = Class.forName("com.charles.reflectDemo.reflectDemo");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Method[] methods=demoClazz.getMethods();
for(Method method:methods)
{
System.out.println(method);
}
}
public static void demo3() //获取本类所有的方法,包括私有
{
Class> demoClazz=null;
try {
demoClazz = Class.forName("com.charles.reflectDemo.reflectDemo");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Method[] methods=demoClazz.getDeclaredMethods();
for(Method method:methods)
{
System.out.println(method);
}
}
public static void demo4() //获取所有公有字段
{
Class> demoClazz=null;
try {
demoClazz = Class.forName("com.charles.reflectDemo.reflectDemo");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Field[] fields=demoClazz.getFields();
for(Field field:fields)
{
System.out.println(field);
}
}
public static void demo5() //获取所有声明字段,包括私有
{
Class> demoClazz=null;
try {
demoClazz = Class.forName("com.charles.reflectDemo.reflectDemo");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Field[] fields=demoClazz.getDeclaredFields();
for(Field field:fields)
{
System.out.println(field);
}
}
public static void demo6() //获取所有构造函数
{
Class> demoClazz=null;
try {
demoClazz = Class.forName("com.charles.reflectDemo.reflectDemo");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Constructor[] constructors=demoClazz.getConstructors();
for(Constructor constructor:constructors)
{
System.out.println(constructor);
}
}
public static void demo7() //获取超类
{
Class> demoClazz=null;
try {
demoClazz = Class.forName("com.charles.reflectDemo.reflectDemo");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Class> className=demoClazz.getSuperclass();//超类只有一个,单继承
System.out.println(className);
}
public static void demo8() //获取实现的接口
{
Class> demoClazz=null;
try {
demoClazz = Class.forName("com.charles.reflectDemo.reflectDemo");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Class>[] inter=demoClazz.getInterfaces();
for(Object inters:inter)
{
System.out.println(inters);
}
}
public static void demo9() //获取该类对象
{
Class> demoClazz=null;
try {
demoClazz = Class.forName("com.charles.reflectDemo.reflectDemo");
Object demo=demoClazz.newInstance();
reflectDemo demo1= (reflectDemo)demo;
demo1.myInterface1Method();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}