目录
一、代码准备
1.1、Creature(父类)
public class Creature<T> implements Serializable {
private char gender;
public double weight;
private void breath(){
System.out.println("呼吸");
}
public void eat(){
System.out.println("用餐");
}
}
1.2、MyAnnotation(注解)
@Target({TYPE, FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() default "hello";
}
1.3、MyInterface(接口)
public interface MyInterface {
void info();
}
1.4、Person
@MyAnnotation(value = "hi")
public class Person extends Creature<String> implements Comparable<String> ,MyInterface {
private String name;
int age;
public int id;
private static String addr;
public Person() throws NoSuchMethodException,NullPointerException{
System.out.println("无参构造函数");
}
@MyAnnotation(value = "abc")
private Person(String name ){
this.name = name ;
}
Person(String name ,int age) throws NullPointerException{
this.name =name ;
this.age = age ;
System.out.println("两个参数");
}
@MyAnnotation
private String show(String nation)throws NullPointerException{
System.out.println("我的国籍是:"+nation);
return nation;
}
public String display(String interests) throws NullPointerException,NoSuchMethodException{
return interests;
}
public static String showAddr(String s,int age){
return s+age;
}
/**
*
* @param interests
* @param age
* @return
*/
String displays(String interests,int age){
return interests+age;
}
@Override
public void info() {
System.out.println("我是一个人");
}
@Override
public int compareTo(String o) {
return 0;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", id=" + id +
'}';
}
}
二、获取属性
private static void test2() {
Class cl= Person.class;
Field[] fields = cl.getDeclaredFields();
for (Field f: fields) {
//获取权限
int modifiers = f.getModifiers();
System.out.print(Modifier.toString(modifiers));
//获取返回类型
Class<?> type = f.getType();
System.out.print(" "+type.getName()+" ");
//获取属性名称
String name = f.getName();
System.out.print(name);
System.out.println();
}
/* 结果:
private java.lang.String name
int age
public int id
private static java.lang.String addr
*/
}
private static void test1() {
Class cl= Person.class;
//获取当前运行时类及其父类中声明为public访问权限的属性
Field[] fields = cl.getFields();
for (Field f: fields) {
System.out.println(f);
}
System.out.println("*******************************");
//获取当前运行类的所有属性包括private权限的,但是父类的所有属性是无法获取的
Field[] declaredFields = cl.getDeclaredFields();
for(Field f:declaredFields){
System.out.println(f);
}
/* 结果:
public int com.lcg.javabase.reflect.java.Person.id
public double com.lcg.javabase.reflect.java.Creature.weight
*******************************
private java.lang.String com.lcg.javabase.reflect.java.Person.name
int com.lcg.javabase.reflect.java.Person.age
public int com.lcg.javabase.reflect.java.Person.id
private static java.lang.String com.lcg.javabase.reflect.java.Person.addr*/
}
三、获取方法
private static void test2() {
Class cl = Person.class;
Method[] declaredMethods = cl.getDeclaredMethods();
for (Method m:declaredMethods ) {
//获取注解
Annotation[] annotations = m.getAnnotations();
for (Annotation a:annotations) {
System.out.println(a);
}
//权限修饰符
System.out.print(Modifier.toString(m.getModifiers())+" ");
//获取返回值
Class<?> returnType = m.getReturnType();
System.out.print(returnType.getName()+" ");
//获取方法名
String name = m.getName();
System.out.print(name);
//获取方法参数类型
System.out.print("(");
Class<?>[] parameterTypes = m.getParameterTypes();
//获取参数的名字没什么意义,输出后为arg0,arg1,,,,所以不如自己定义
// Parameter[] parameters = m.getParameters();
if(parameterTypes.length>0){
for (int i = 0; i <parameterTypes.length ; i++) {
if(i==parameterTypes.length-1){
System.out.print(parameterTypes[i].getName() + " arg" +i );
}else {
System.out.print(parameterTypes[i].getName() + " arg" +i +",");
}
}
}
System.out.print(") ");
//获取异常
Class<?>[] exceptionTypes = m.getExceptionTypes();
if(exceptionTypes.length>0){
System.out.print("throws ");
for (int i = 0; i <exceptionTypes.length ; i++) {
if((exceptionTypes.length-1)==i){
System.out.print(exceptionTypes[i].getName());
}else{
System.out.print(exceptionTypes[i].getName()+", ");
}
}
}
System.out.println("\n");
}
/* 结果:
public java.lang.String toString()
public int compareTo(java.lang.String arg0)
public volatile int compareTo(java.lang.Object arg0)
public static java.lang.String showAddr(java.lang.String arg0,int arg1)
public void info()
@com.lcg.javabase.reflect.java.MyAnnotation(value=hello)
private java.lang.String show(java.lang.String arg0) throws java.lang.NullPointerException
public java.lang.String display(java.lang.String arg0) throws java.lang.NullPointerException, java.lang.NoSuchMethodException
java.lang.String displays(java.lang.String arg0,int arg1)*/
}
private static void test1() {
Class cl = Person.class;
//获取运行时类及其父类中声明为public权限的方法
Method[] methods = cl.getMethods();
for(Method method : methods){
System.out.println(method);
}
System.out.println("*******************************");
//获取当前运行时类的所有方法,不包括父类的
Method[] declaredMethods = cl.getDeclaredMethods();
for(Method method : declaredMethods){
System.out.println(method);
}
/*结果:
public java.lang.String com.lcg.javabase.reflect.java.Person.toString()
public int com.lcg.javabase.reflect.java.Person.compareTo(java.lang.String)
public int com.lcg.javabase.reflect.java.Person.compareTo(java.lang.Object)
public void com.lcg.javabase.reflect.java.Person.info()
public static java.lang.String com.lcg.javabase.reflect.java.Person.showAddr(java.lang.String,int)
public java.lang.String com.lcg.javabase.reflect.java.Person.display(java.lang.String) throws java.lang.NullPointerException,java.lang.NoSuchMethodException
public void com.lcg.javabase.reflect.java.Creature.eat()
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
*******************************
public java.lang.String com.lcg.javabase.reflect.java.Person.toString()
public int com.lcg.javabase.reflect.java.Person.compareTo(java.lang.String)
public int com.lcg.javabase.reflect.java.Person.compareTo(java.lang.Object)
private java.lang.String com.lcg.javabase.reflect.java.Person.show(java.lang.String) throws java.lang.NullPointerException
java.lang.String com.lcg.javabase.reflect.java.Person.displays(java.lang.String,int)
public void com.lcg.javabase.reflect.java.Person.info()
public static java.lang.String com.lcg.javabase.reflect.java.Person.showAddr(java.lang.String,int)
public java.lang.String com.lcg.javabase.reflect.java.Person.display(java.lang.String) throws java.lang.NullPointerException,java.lang.NoSuchMethodException
*/
}
四、获取构造器及类中的其他属性
private static void test3() {
Class cl= Person.class;
//获取类上的注释
Annotation[] annotations = cl.getAnnotations();
for (Annotation a :
annotations) {
System.out.println(a);
}
//获取类的接口
Class[] interfaces = cl.getInterfaces();
for (Class i:
interfaces) {
System.out.println(i.getName());
}
//获取包
Package aPackage = cl.getPackage();
System.out.println(aPackage.getName());
/*结果:
@com.lcg.javabase.reflect.java.MyAnnotation(value=hi)
java.lang.Comparable
com.lcg.javabase.reflect.java.MyInterface
com.lcg.javabase.reflect.java
*/
}
private static void supperClassTest() {
Class cl= Person.class;
//获取父类
Class superclass = cl.getSuperclass();
System.out.println(superclass);
//获取带着泛型的父类
Type genericSuperclass = cl.getGenericSuperclass();
System.out.println(genericSuperclass);
//获取泛型类型
ParameterizedType parameterizedType = (ParameterizedType)genericSuperclass;
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
for (Type t:
actualTypeArguments) {
System.out.println(t.getTypeName());
}
/*结果:
class com.lcg.javabase.reflect.java.Creature
com.lcg.javabase.reflect.java.Creature<java.lang.String>
java.lang.String*/
}
private static void constructorTest() {
Class cl= Person.class;
//获取运行时类声明为public权限构造器(不包括父类的)
Constructor[] constructors = cl.getConstructors();
for(Constructor c :constructors){
System.out.println(c.getName());
}
System.out.println("++++++++++++++++++++++++++++");
//获取运行时类的所有构造器(不包括父类的)
Constructor[] declaredConstructors = cl.getDeclaredConstructors();
for(Constructor c :declaredConstructors){
//获取注解
Annotation[] annotations = c.getAnnotations();
for (Annotation a:annotations) {
System.out.println(a);
}
//权限修饰符
System.out.print(Modifier.toString(c.getModifiers())+" ");
System.out.print (c.getName());
//获取方法参数类型
System.out.print("(");
Class<?>[] parameterTypes = c.getParameterTypes();
//获取参数的名字没什么意义,输出后为arg0,arg1,,,,所以不如自己定义
//Parameter[] parameters = c.getParameters();
if(parameterTypes.length>0){
for (int i = 0; i <parameterTypes.length ; i++) {
if(i==parameterTypes.length-1){
System.out.print(parameterTypes[i].getName() + " arg" +i );
}else {
System.out.print(parameterTypes[i].getName() + " arg" +i +",");
}
}
}
System.out.print(") ");
//获取异常
Class<?>[] exceptionTypes = c.getExceptionTypes();
if(exceptionTypes.length>0){
System.out.print("throws ");
for (int i = 0; i <exceptionTypes.length ; i++) {
if((exceptionTypes.length-1)==i){
System.out.print(exceptionTypes[i].getName());
}else{
System.out.print(exceptionTypes[i].getName()+", ");
}
}
}
System.out.println("\n");
}
/* 结果:
com.lcg.javabase.reflect.java.Person
++++++++++++++++++++++++++++
com.lcg.javabase.reflect.java.Person(java.lang.String arg0,int arg1) throws java.lang.NullPointerException
@com.lcg.javabase.reflect.java.MyAnnotation(value=abc)
private com.lcg.javabase.reflect.java.Person(java.lang.String arg0)
public com.lcg.javabase.reflect.java.Person() throws java.lang.NoSuchMethodException, java.lang.NullPointerException*/
}
五、操作属性
//操作运行时类的指定属性
private static void test2() throws InstantiationException, IllegalAccessException, NoSuchFieldException {
Class cl = Person.class;
Person o = (Person) cl.newInstance();
//调用指定的属性该属性必须是声明为public其中包括父类中声明为public的属性
//getDeclaredField与getField的区别与getDeclaredFields和getFields的区别一样
Field name = cl.getDeclaredField("name");
name.setAccessible(true);
//给指定属性赋值
//第一参数:指明设置哪个对象的属性,第二个参数:此属性的值
//如果是静态的属性,那第一个参数可以是null,或者任何对象
//name.set(null,'山东')
name.set(o,"tom");
//获取指定属性的值
Object o1 = name.get(o);
System.out.println(o1);
/* 结果:
无参构造函数
tom*/
}
六、操作方法
private static void test3() throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Class cl = Person.class;
Person o = (Person) cl.newInstance();
//getDeclaredMethod 与getMethod区别与之前getDeclaredMethods和getMethods区别一致
//第一个参数对应的是方法名,后边的参数对应的该方法的形参类型
Method displays = cl.getDeclaredMethod("displays", String.class, int.class);
displays.setAccessible(true);
//第一个参数指明哪个对象的方法,后边的参数为调用该方法所用到的实参
//如果是静态方法,第一个参数可以是null也可以是任何值
Object retu = displays.invoke(o, "玩游戏", 6);
System.out.println(retu);
/* 结果:
无参构造函数
玩游戏6*/
}
七、操作构造函数
private static void test4() throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
Class cl = Person.class;
Constructor constructor = cl.getDeclaredConstructor(String.class,int.class);
constructor.setAccessible(true);
Object o = constructor.newInstance("tom",20);
System.out.println(o);
/*结果:
两个参数
Person{name='tom', age=20, id=0}*/
}