java 通过反射获取类属性结构,类方法,类父类及其泛型,类,接口和包

首先自定义三个类

package reflection1;

public interface MtInterface {
void info();
}
package reflection1;

import java.io.Serializable;

public class Creature<T> implements Serializable {
private char gender;
public double weight; private void breath() {
System.out.println("呼吸");
}
public void eat() {
System.out.println("吃饭");
}
}
package reflection1;

public class Person extends Creature<String> implements Comparable<String>,MtInterface {

    private String name;
int age;
public int id; public Person() {
super();
}
private Person(String name) {
super();
this.name = name;
}
Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Person(String name, int age, int id) {
super();
this.name = name;
this.age = age;
this.id = id;
} private String show(String nation) {
System.out.println("nation="+nation);
return nation;
}
public String display(String interests) {
return interests;
} @Override
public void info() {
System.out.println("我是人");
} @Override
public int compareTo(String o) {
return 0;
} private static void showDesc() {
System.out.println("i am zsben");
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", id=" + id + "]";
} }

然后通过反射获取类属性结构

package reflection2;

import java.lang.module.ModuleDescriptor.Exports.Modifier;
import java.lang.reflect.Field; import org.junit.jupiter.api.Test; import reflection1.*; /*
* 获取当前运行时类的属性结构
* */ public class FieldTest {
@Test
public void test1() {
Class clazz = Person.class; //获取属性结构
//getFields():获取当前运行时类及其父类中所有public的属性
Field [] fields = clazz.getFields();
for(Field f:fields)
System.out.println(f); System.out.println(""); //getDeclaredFields():获得当前运行时类的所有属性,不包含父类的属性,不考虑权限
fields = clazz.getDeclaredFields();
for(Field f:fields)
System.out.println(f); System.out.println("");
} //权限修饰符:数据类型 变量名
@Test
public void test2() {
Class clazz = Person.class;
Field [] fields = clazz.getDeclaredFields();
for(Field f:fields) {
System.out.println(f);
//1.权限修饰符
int modifiers = f.getModifiers();
System.out.println(modifiers); //2.数据类型
Class type = f.getType();
System.out.println(type); //3.变量名
String name = f.getName();
System.out.println(name); System.out.println("");
}
}
}

获取类方法

package reflection2;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method; import org.junit.jupiter.api.Test; import reflection.Person; /*
* 获取运行时类的方法结构
*
* */ public class MethodTest {
@Test
public void test1() {
Class clazz = Person.class;
//getMethods():获取当前类及其父类所有public方法
Method[] methods = clazz.getMethods();
for(Method m:methods) {
System.out.println(m);
}
System.out.print('\n'); //获取当前运行时类中的所有方法
methods = clazz.getDeclaredMethods();
for(Method m:methods) {
System.out.println(m);
}
}
/*
* 权限修饰符,返回值类型,方法名(参数类型1 参数1,参数类型2 参数2,参数类型3 参数3...)
* */
@Test
public void test2() {
//1.获取方法声明的注解
Class clazz = Person.class;
Method[]methods = clazz.getDeclaredMethods();
for(Method m:methods) {
System.out.println(m);
//1.获得方法声明的注解
Annotation[] annos = m.getAnnotations();
for(Annotation a:annos) {
System.out.println(a);
} //2.获取权限修饰符
int modifier = m.getModifiers();
System.out.println(modifier); //3.返回值类型
System.out.println(m.getReturnType().getName()); //4.方法名
System.out.println(m.getName()); //5.形参列表
Class [] parameterTypes = m.getParameterTypes();
if(!(parameterTypes == null && parameterTypes.length==0)) {
for(int i=0;i<parameterTypes.length;i++) {
Class p = parameterTypes[i];
System.out.println(p.getName()+" args_"+i);
}
} //6.抛出的异常
Class [] exceptionTypes = m.getExceptionTypes();
for(Class e:exceptionTypes)
System.out.println(e.getName());
}
} }

父类及其泛型,所在包,接口

package reflection2;

import java.lang.reflect.Constructor;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import reflection1.Person; public class OtherTest {
/*
* 获取构造器结构
* */
@Test
public void test1() {
Class clazz = Person.class;
Constructor[] constructors = clazz.getConstructors();
for(Constructor c:constructors)
System.out.println(c); System.out.println(); constructors = clazz.getDeclaredConstructors();
for(Constructor c:constructors)
System.out.println(c);
}
/*
* 获取运行时类的父类
* */
@Test
public void test2() {
Class clazz = Person.class;
Class superClass = clazz.getSuperclass();
System.out.println(superClass); } /*
* 获取运行时带泛型的父类
* */
@Test
public void test3() {
Class clazz = Person.class;
Type superClass = clazz.getGenericSuperclass();
System.out.println(superClass); }
/*
* 获取运行时带泛型的父类的泛型
* */
@Test
public void test4() {
Class clazz = Person.class;
Type superClass = clazz.getGenericSuperclass();
ParameterizedType paramType = (ParameterizedType)superClass;
//获取泛型类型
Type[] types = paramType.getActualTypeArguments();
System.out.println(types[0].getTypeName());
} /*
* 获取运行时类的接口
* */
@Test
public void test5() {
Class clazz = Person.class;
Class[] interfaces = clazz.getInterfaces();
for(Class c:interfaces)
System.out.println(c); System.out.println();
Class[]interfaces1 = clazz.getSuperclass().getInterfaces();
for(Class c:interfaces1)
System.out.println(c);
} /*
* 获取类运行时所在包
* */
@Test
public void test6() {
Class clazz = Person.class;
Package package1 = clazz.getPackage();
System.out.println(package1);
}
}
上一篇:poj 1664


下一篇:牛客网Java刷题知识点之什么是代码块、普通代码块、静态代码块、同步代码块、构造代码块以及执行顺序