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
{
    protected boolean gender;
    public int id;
    public void breath()
    {
        System.out.println("Breath");
    }
    private void info()
    {
        System.out.println("info");
    }
}

public class Creature
{
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 implements Comparable,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 implements Comparable,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() {

}
}

你可能感兴趣的:(JAVA随手写,开发语言,网络,java,数据结构,intellij-idea)