反射-创建运行时类的对象

获取运行时类的完整结构
通过反射获取运行时类的完整结构
Field、Method、Constructor、Superclass、Interface、Annotation
实现的全部接口
所继承的父类
全部的构造器
全部的方法
全部的Field
注解

 

package 反射;//获得类的信息

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Text1 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodError, NoSuchFieldException, NoSuchMethodException {
        Class c1=Class.forName("反射.User");//正常情况要写会部的路径,包名
        
        //通过对象获得包名加类名
        //User user=new User();
        //c1=user.getClass();
        
        //获得类的名字
        System.out.println(c1.getName());//包名+类名
        System.out.println(c1.getSimpleName());//获得类名
        //获得类的属性
       Field[] fields= c1.getFields();//获得类的全部属性,只能找到puble属性
       
        fields=c1.getDeclaredFields();//可以找全部的属性
       //因为是多个数据且是数组所以要遍历
       for(Field field:fields){
           System.out.println(field);
       }
       //如果知道属性是什么样的,可以通过指定的属性获得
       // Field name=c1.getField("name");//获得指定属性的值,这个是会报异常的,因为他只能找到public权限的属性,如果找不到会报出NoSuchFieldException异常
       // System.out.println(name);
        Field name1=c1.getDeclaredField("name"); //获得指定属性的值,可以找到全部权限的属性,包括私有
        System.out.println(name1);
        
        //获得类的方法
        Method[] methods=c1.getMethods();//获得本类及其父类全部的public方法,其他权限的不获得
        //遍历输出
        for(Method method:methods){
            System.out.println("正常的:"+method);
        }
         methods=c1.getDeclaredMethods();//获得本类的所有方法包括私有的,不获得父类的方法
        for(Method method:methods){
            System.out.println("getDeclaredMethodds:"+method);
        }
        
        //获得指定方法:需要传一个name还要传一个class
        //这个为什么要写参数,因为java有重载机制,如果不写参数,那就不能准确的读取到,我们需要的方法
       Method getName=c1.getMethod("getName",null);//因为getName的参数列表是没有的,所以写Null就可以了
       
        Method setName=c1.getMethod("setName",String.class);//因为setName的参数列表是需要一个String类型的值,所以我们精心String.class即可,如果是int,写int.class,以此类推
        System.out.println(getName);
        System.out.println(setName);
        
        
        //获得指定的构造器
        Constructor[] constrctors=c1.getConstructors();//获得所有的构造器,只能获得public类型的
            for(Constructor constrctor:constrctors){
                System.out.println(constrctor);
            }
        constrctors=c1.getDeclaredConstructors();//可以获得全部权限的方法
        for(Constructor constrctor:constrctors){
                System.out.println(constrctor);
            }
            
            //获得指定的构造器
        Constructor declaredConstrctor=c1.getDeclaredConstructor(String.class,int.class,int.class);
            System.out.println("指定:"+declaredConstrctor);
    }
}












//实体类:pojo,entity:来表示实体类
class User{
    private String name;
    private int id;
    private int age;
    
    public User(){
        
    }
    
    public User(String name,int id,int age){
        this.name=name;
        this.id=id;
        this.age=age;
    }
    
    
    public void setName(String name){
        this.name=name;
    }
    
    public String getName(){
        return name;
    }
    //------------------
    
     public void setId(int id){
        this.id=id;
    }
    
    public int getId(){
        return id;
    }
    
    //--------------
       public void setAge(int age){
        this.age=age;
    }
    
    public int getAge(){
        return age;
    }
    
    //输出方法
    public String toString(){
        return "User{"+"name="+name+"id="+id+"age="+age+"}";

}
}

 

posted @ 2022-06-07 16:13  hollg  阅读(52)  评论(0)    收藏  举报