Field、Method、Constructor、Superclass、Interface、Annotation
//父类
package com.ztc.reflect;
public class Creature<T> {
public double weight;
public void breath(){
System.out.println("呼吸");
}
}
//子类
package com.ztc.reflect;
@MyAnnotation(value="ztc")
public class Person extends Creature<String> implements Comparable,MyInterface{
public String name;
private int age;
int id;
public Person() {
super();
}
public Person(String name) {
super();
this.name = name;
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
@Override
public int compareTo(Object arg0) {
// TODO Auto-generated method stub
return 0;
}
@MyAnnotation(value="show")
public void show(){
System.out.println("我是一个人");
}
public void display(String nation) throws Exception{
System.out.println("我的国籍是:"+nation);
}
class Brid{
}
}
Field:
package com.ztc.reflect;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import org.junit.Test;
public class TestField {
//获得对应的运行时类的属性
@Test
public void test1(){
Class clazz=Person.class;
//1.getFields()只能申请到运行时类中声明为public的属性(包含父类中的属性)
Field[] fields=clazz.getFields();
// for(int i=0;i<fields.length;i++){
// System.out.println(fields[i]);
// }
for(Field f:fields){
System.out.println(f);
}
System.out.println();
//2.getDeclaredFields()获取所有的属性(只能获得该类本身的属性,不包含父类)
Field[] fields2=clazz.getDeclaredFields();
for(Field f:fields2){
System.out.println(f);
}
}
//权限修饰符 、变量类型、变量名
//获取属性各个部分的内容
@Test
public void test2(){
Class clazz=Person.class;
Field[] fields2=clazz.getDeclaredFields();
for(Field f:fields2){
//1.获取每个属性的权限修饰符
//返回值为int,0位default,1为public,2为private。。。
int i=f.getModifiers();
//把int转为对应的string
String str1=Modifier.toString(i);
System.out.println(i+" "+str1);
//2.获取每个属性的变量类型
Class type=f.getType();
System.out.println(type.getName());
//3.获取每个属性的变量名
System.out.println(f.getName());
}
}
}
Method:
package com.ztc.reflect;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import org.junit.Test;
public class TestMethod {
//获取运行时类的方法
@Test
public void test1(){
Class clazz=Person.class;
//getMethods获得该类或父类中的public方法
Method[] m1=clazz.getMethods();
for(Method m:m1){
System.out.println(m);
}
System.out.println("-----------------------------------------------------------");
//getDeclaredMethods获得类本身的所有方法
Method[] m2=clazz.getDeclaredMethods();
for(Method m:m2){
System.out.println(m);
}
}
//获取方法的注解、权限修饰符、返回值类型、变量名、形参列表、异常
@Test
public void test2(){
Class clazz=Person.class;
Method[] m2=clazz.getDeclaredMethods();
for(Method m:m2){
//1.注解
Annotation[] a1 = m.getAnnotations();
for(Annotation a:a1){
System.out.println("Method Annotation"+a);
}
//2.权限修饰符
String str=Modifier.toString(m.getModifiers());
System.out.println("Method Modifier: "+str);
//3.返回值类型
Class returnType=m.getReturnType();
System.out.println("Method ReturnType: "+returnType.getName());
//4.变量名
String name=m.getName();
System.out.println("Method name: "+name);
//5.形参类型列表
Class[] params=m.getParameterTypes();
for(Class p:params){
System.out.println("Method param: "+p.getName());
}
//6.异常
Class[] exTypes=m.getExceptionTypes();
for(Class e:exTypes){
System.out.println("Method exType: "+e.getName());
}
}
}
}
Constructor:
package com.ztc.reflect;
import java.lang.reflect.Constructor;
import org.junit.Test;
public class TestConstructor {
@Test
public void test1() throws Exception{
String className="com.ztc.reflect.Person";
Class clazz=Class.forName(className);
Object obj=clazz.newInstance();
Person p=(Person) obj;
System.out.println(p);
}
//获取构造器
@Test
public void test2() throws Exception{
String className="com.ztc.reflect.Person";
Class clazz=Class.forName(className);
Constructor[] cons=clazz.getDeclaredConstructors();
for(Constructor c:cons){
System.out.println(c);
}
}
}
其他:
package com.ztc.reflect;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import org.junit.Test;
public class TestOthers {
//获取运行时类的父类
@Test
public void test1(){
Class clazz=Person.class;
Class superClass=clazz.getSuperclass();
System.out.println(superClass);
}
//获取带泛型的父类
@Test
public void test2(){
Class clazz=Person.class;
Type type=clazz.getGenericSuperclass();
System.out.println(type);
}
//父类的泛型
@Test
public void test3(){
Class clazz=Person.class;
Type type=clazz.getGenericSuperclass();
//强转成子类
ParameterizedType param=(ParameterizedType) type;
Type[] ars=param.getActualTypeArguments();
System.out.println(ars[0]);
}
//获取实现的接口(接口的父接口是获取不到的)
@Test
public void test4(){
Class clazz=Person.class;
Class[] interfaces=clazz.getInterfaces();
for(Class i:interfaces){
System.out.println(i);
}
}
//获取所在的包
@Test
public void test5(){
Class clazz=Person.class;
Package pack=clazz.getPackage();
System.out.println(pack);
}
//获取类的注解
@Test
public void test6(){
Class clazz=Person.class;
Annotation[] anns=clazz.getAnnotations();
for(Annotation a:anns){
System.out.println(a);
}
}
}