首页 > 其他分享 >反射_Class对象功能概述与反射_Class对象功能_获取Field

反射_Class对象功能概述与反射_Class对象功能_获取Field

时间:2022-10-13 11:22:49浏览次数:54  
标签:反射 name ... System Class Field constructor println Method

反射_Class对象功能概述

  

Constructor:构造方法
  创建对象;
    
T newInstance​(Object... initargs) 
使用由此 Constructor对象表示的构造函数,使用指定的初始化参数来创建和初始化构造函数的声明类的新实例。

  如果使用空参数构造方法创建对象,操作可以简化:class对象 newInttance方法




package day01.Demo01_Day017.da;

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


/*
        class对象功能∶
获取功能∶
1.获取成员变们
Field[] getFields()
Field getField(string name)
Field[] getDeclaredFields()
Field getDeclaredField(string name)
2.获取构造方法们
constructor<?>[]getconstructors()
constructor<T> getconstructor(类<?>... parameterTypes)
constructor<T> getDeclaredconstructor(类<?>... parameterTypes)
constructor<?>[getDeclaredconstructors()
3。获取成员方法们︰
Method[] getMethods()
Method getMethod(string name,类<?>... parameterTypes)
Method[] getDeclaredMethods()
Method getDeclaredMethod(string name,类<?>... parameterTypes)
4.获取类名
String getName()
     */
public class ReflectDemo2 {
        public static void main(String[] args) throws Exception{
            //1.获取Person的class对象
            Class<Person> personClass = Person.class;
            /*
            2.获取构造方法们
                constructor<?>[]getconstructors()
                constructor<T> getconstructor(类<?>... parameterTypes)
                constructor<T> getDeclaredconstructor(类<?>... parameterTypes)
                constructor<?>[getDeclaredconstructors()
             */

            //constructor<?>[]getconstructors()
            Constructor<?>[] constructors = personClass.getConstructors();
            System.out.println(constructors);

            System.out.println("**************************");
            //constructor<T> getconstructor(类<?>... parameterTypes)
            Constructor<Person> constructor1 = personClass.getConstructor(String.class, int.class);
            System.out.println(constructor1);
            //创建对象
            Person person1 = constructor1.newInstance("莫凡", 20);
            System.out.println(person1);

            System.out.println("**************************");
            Constructor<Person> constructor2 = personClass.getConstructor();
            System.out.println(constructor2);
            //创建对象
            Person person2 = constructor2.newInstance();
            System.out.println(person2);
            System.out.println("*********************");
            Person o = personClass.newInstance();
            System.out.println(o);


            /*
                其中构造器方法也是可以完成暴力反射
             */
            //constructor1.setAccessible(true);

        }
}

 

反射_Class对象功能_获取Field

 

Method:方法对象
  执行方法:
   Object invoke​(Object obj, Object... args)在具有指定参数的指定对象上调用此 方法对象表示的基础方法。
获取方法名称
   String getName;获取方法名
package day01.Demo01_Day017.da;

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


/*
        class对象功能∶
获取功能∶
1.获取成员变们
Field[] getFields()
Field getField(string name)
Field[] getDeclaredFields()
Field getDeclaredField(string name)
2.获取构造方法们
constructor<?>[]getconstructors()
constructor<T> getconstructor(类<?>... parameterTypes)
constructor<T> getDeclaredconstructor(类<?>... parameterTypes)
constructor<?>[getDeclaredconstructors()
3。获取成员方法们︰
Method[] getMethods()
Method getMethod(string name,类<?>... parameterTypes)
Method[] getDeclaredMethods()
Method getDeclaredMethod(string name,类<?>... parameterTypes)
4.获取类名
String getName()
     */
public class ReflectDemo3 {
        public static void main(String[] args) throws Exception{
            //1.获取Person的class对象
            Class<Person> personClass = Person.class;
            /*
           3.获取成员方法们︰
                Method[] getMethods()
                Method getMethod(string name,类<?>... parameterTypes)
                Method[] getDeclaredMethods()
                Method getDeclaredMethod(string name,类<?>... parameterTypes)
             */
            //获取指定名称的方法
            Method eat_method1 = personClass.getMethod("eat");
            Person p = new Person();
            //执行方法
            eat_method1.invoke(p);


            Method eat_method2 = personClass.getMethod("eat", String.class);
            //执行方法
            eat_method2.invoke(p,"九菜一汤");

            System.out.println("********************");

            //获取所有public修饰的方法
            Method[] methods = personClass.getMethods();
            for (Method method : methods) {
                System.out.println(method);
                String name = method.getName();
                System.out.println(name);
                method.setAccessible(true);
            }
            //获取类名
            String name = personClass.getName();
            System.out.println(name);//day01.Demo01_Day017.da.Person
        }
}

 

 
 

 

标签:反射,name,...,System,Class,Field,constructor,println,Method
From: https://www.cnblogs.com/x3449/p/16787552.html

相关文章