JAVA使用反射机制获取类的对象和运行时类的完整结构-----JAVA

import org.junit.jupiter.api.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class NewInstanceTest
{
    @Test
    public void Test() throws Exception
    {
        Class c = Person.class;
        //通过Class的实例调用newInstance()方法即可创建类的实例对象
        Person p = (Person)c.newInstance();
        System.out.println(p);
        //要想创建对象成功,需要运行时类提供一个空参的构造器,其权限要足够,才能使用newInstance方法
        //JDK9以后我们用Constructor类调用newInstance
        //只能获取运行时类及父类的public权限的属性
        Field[] fields = c.getFields();
        for(Field f : fields)
        {
            System.out.println(f);
        }
        //获取运行时类的所有的属性
        System.out.println();
        fields = c.getDeclaredFields();
        for(Field field : fields)
        {
            //获取权限修饰符号
            int m = field.getModifiers();
            System.out.print(Modifier.toString((m)));
            //获取数据类型
            Class type = field.getType();
            System.out.print(type.getName());
            //变量名
            String string = field.getName();
            System.out.print(string);
            System.out.println();
            System.out.println(field);
        }
    }
    public static void main(String[] args)
    {
        Class c = Person.class;
        Method[] d = c.getDeclaredMethods();
        for(Method m : d)
        {
            //获取方法注解信息
            Annotation[] annotations = m.getAnnotations();
            for(Annotation annotation : annotations)
            {
                System.out.println(annotation);
            }
            //权限修饰符
            System.out.println(Modifier.toString(m.getModifiers()));
            //返回值类型
            System.out.println(m.getReturnType().getName());
            //形参列表
            Class[] parameter = m.getParameterTypes();
            if(!(parameter == null && parameter.length == 0))
            {
                for (int i = 0; i < parameter.length; i++)
                {
                    if(i == parameter.length - 1)
                    {
                        System.out.print(parameter[i].getName());
                        break;
                    }
                    System.out.print(parameter[i].getName());
                }
            }
            //抛出的异常
            Class[] e = m.getExceptionTypes();
            if(e.length > 0)
            {
                System.out.println("throws");
                for(int i = 0;i < e.length;i++)
                {
                    if(i == e.length - 1)
                    {
                        System.out.println(e[i].getName());
                        break;
                    }
                    System.out.println(e[i].getName());
                }
            }
            System.out.println();
        }
    }
}

import org.junit.jupiter.api.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class NewInstanceTest
{
@Test
public void Test() throws Exception
{
Class c = Person.class;
//通过Class的实例调用newInstance()方法即可创建类的实例对象
Person p = (Person)c.newInstance();
System.out.println(p);
//要想创建对象成功,需要运行时类提供一个空参的构造器,其权限要足够,才能使用newInstance方法
//JDK9以后我们用Constructor类调用newInstance
//只能获取运行时类及父类的public权限的属性
Field[] fields = c.getFields();
for(Field f : fields)
{
System.out.println(f);
}
//获取运行时类的所有的属性
System.out.println();
fields = c.getDeclaredFields();
for(Field field : fields)
{
//获取权限修饰符号
int m = field.getModifiers();
System.out.print(Modifier.toString((m)));
//获取数据类型
Class type = field.getType();
System.out.print(type.getName());
//变量名
String string = field.getName();
System.out.print(string);
System.out.println();
System.out.println(field);
}
}
public static void main(String[] args)
{
Class c = Person.class;
Method[] d = c.getDeclaredMethods();
for(Method m : d)
{
//获取方法注解信息
Annotation[] annotations = m.getAnnotations();
for(Annotation annotation : annotations)
{
System.out.println(annotation);
}
//权限修饰符
System.out.println(Modifier.toString(m.getModifiers()));
//返回值类型
System.out.println(m.getReturnType().getName());
//形参列表
Class[] parameter = m.getParameterTypes();
if(!(parameter == null && parameter.length == 0))
{
for (int i = 0; i < parameter.length; i++)
{
if(i == parameter.length - 1)
{
System.out.print(parameter[i].getName());
break;
}
System.out.print(parameter[i].getName());
}
}
//抛出的异常
Class[] e = m.getExceptionTypes();
if(e.length > 0)
{
System.out.println("throws");
for(int i = 0;i < e.length;i++)
{
if(i == e.length - 1)
{
System.out.println(e[i].getName());
break;
}
System.out.println(e[i].getName());
}
}
System.out.println();
}
}
}

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class OtherTest
{
    public static void main(String[] args) throws Exception
    {
        //获取父类
        Class c = Class.forName("Person");
        Class s = c.getSuperclass();
        System.out.println(s);
        //获取带泛型的父类
        Type ss = c.getGenericSuperclass();
        System.out.println(ss);
        //获取运行时类实现的接口
        Type[] types = c.getInterfaces();
        for(Type type : types)
        {
            System.out.println(type.getTypeName());
        }
        //获取运行时类所在的包
        System.out.println(c.getPackageName());
        System.out.println(c.getPackage().toString());
        //获取运行时类的父类的泛型,Type是一个接口,Class实现了此接口
        ParameterizedType parameterizedType = (ParameterizedType) ss;
        Type[] a = parameterizedType.getActualTypeArguments();
        for(Type type : a)
        {
            System.out.println(((Class)type).getName());
        }
        //如果父类是带泛型的,则可以强转为ParameterizedType类型,调用getActualTypeArguments()得到数组
    }
}

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class OtherTest
{
public static void main(String[] args) throws Exception
{
//获取父类
Class c = Class.forName("Person");
Class s = c.getSuperclass();
System.out.println(s);
//获取带泛型的父类
Type ss = c.getGenericSuperclass();
System.out.println(ss);
//获取运行时类实现的接口
Type[] types = c.getInterfaces();
for(Type type : types)
{
System.out.println(type.getTypeName());
}
//获取运行时类所在的包
System.out.println(c.getPackageName());
System.out.println(c.getPackage().toString());
//获取运行时类的父类的泛型,Type是一个接口,Class实现了此接口
ParameterizedType parameterizedType = (ParameterizedType) ss;
Type[] a = parameterizedType.getActualTypeArguments();
for(Type type : a)
{
System.out.println(((Class)type).getName());
}
//如果父类是带泛型的,则可以强转为ParameterizedType类型,调用getActualTypeArguments()得到数组
}
}

public class Creature<T>
{
    protected boolean gender;
    public int id;
    public void breath()
    {
        System.out.println("Breath");
    }
    private void info()
    {
        System.out.println("info");
    }
}

public class Creature<T>
{
protected boolean gender;
public int id;
public void breath()
{
System.out.println("Breath");
}
private void info()
{
System.out.println("info");
}
}

public interface MyInterface
{
    void method();
}

public interface MyInterface
{
void method();
}

public class Person extends Creature<String> implements Comparable<Person>,MyInterface
{
    private String name;
    public int age = 1;
    private static String info;
    public Person()
    {
        System.out.println("Person");
    }
    protected Person(int age)
    {
        this.age = age;
    }
    private Person(String name,int age)
    {
        this.age = age;
        this.name = name;
    }
    public void show() throws RuntimeException,ClassNotFoundException
    {
        System.out.println("是个人");
    }
    private String showNation(String nation,int age)
    {
        System.out.println("nation");
        return nation + age;
    }
    @Override
    public String toString()
    {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    public int compareTo(Person person)
    {
        return 0;
    }
    public static void showInfo()
    {
        System.out.println("人");
    }

    @Override
    public void method() {

    }
}

public class Person extends Creature<String> implements Comparable<Person>,MyInterface
{
private String name;
public int age = 1;
private static String info;
public Person()
{
System.out.println("Person");
}
protected Person(int age)
{
this.age = age;
}
private Person(String name,int age)
{
this.age = age;
this.name = name;
}
public void show() throws RuntimeException,ClassNotFoundException
{
System.out.println("是个人");
}
private String showNation(String nation,int age)
{
System.out.println("nation");
return nation + age;
}
@Override
public String toString()
{
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public int compareTo(Person person)
{
return 0;
}
public static void showInfo()
{
System.out.println("人");
}

@Override
public void method() {

}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旧约Alatus

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值