创建运行时类的指定

创建运行时类的指定:

package Reflection3;

import Reflection2.Person;

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

/*
调用运行时类中指定的结构:属性,方法,构造器
*/

public class ReflectionTest
{
   public void testField() throws Exception
  {
       Class clazz= Person.class;
       //创建运行时类的对象
          Person person=(Person)clazz.newInstance();
       //获取指定的属性:要求运行时类中属性声明为public
       //通常不采用此方法
       Field id = clazz.getField("id");
       /*
       //设置当前属性的值
        set():参数1:指明设置哪个对象的属性 参数2:将此属性值设置为多少
        */
       id.set(person,1001);
       /*
       获取当前属性的值
       get():参数1:获取哪个对象的当前属性值
        */
      int pd=(int) id.get(person);
       System.out.println(pd);
  }

   /*
           如何操作运行时类中的指定的属性---需要掌握(开发中用这种方法)
    */
public void testField1() throws Exception
{

   Class clazz= Person.class;
   //创建运行时类的对象
   Person person=(Person)clazz.newInstance();

   //1.getDeclaredField(String fieldName):获取运行时类中指定变量名的属性
   Field name = clazz.getDeclaredField("name");
   //2.setAccessible(true):保证当前属性是可访问的
   name.setAccessible(true);
   //3.获取,设置指定对象的此属性值
         name.set(person,"Tom");
   System.out.println(name.get(person));
}


/*
 如何操作运行时类中的指定的方法---需掌握

*/
public void testtMethod() throws Exception
{
   Class clazz= Person.class;
   //创建运行时类的对象(静态不需要写)
   Person person=(Person)clazz.newInstance();
   /*
    1. 获取指定的某个方法
     getDeclaredMethod(): 参数1:指明获取方法的名称 参数2:指明获取的方法的形参列表
    */
   Method show = clazz.getDeclaredMethod("show", String.class);
   //2. 保证当前方法是可访问的
   show.setAccessible(true);
  /*
    3. 调用方法的invoke():参数1:方法的调用者   参数2 :给方法形参赋值的实参
    invoke()的返回值即为对应类中调用的方法返回值
   */
 // show.invoke(person,"CHN");//String nation=porson.show("CHN");
   Object chn = show.invoke(person, "CHN");
   System.out.println(chn);

   System.out.println("*********如何调用静态方法***************");

   //private static void showDesc()
   Method showSesc = clazz.getDeclaredMethod("showSesc");
            showSesc.setAccessible(true);
            //如果调用的运行时类的方法没有返回值,则此invoke()返回null
            showSesc.invoke(Person.class);
 // Object invoke = showSesc.invoke(Person.class);//因为是void想接就接(必须得有返回值,要是没有就null)

}

/*
如何调用运行时类中的指定的构造器 不常用这个方法
*/
public void testConstructor() throws Exception
{
   Class clazz=Person.class;
   // private Person(String name)
   /*
   获取指定的构造器
   getDeclaredConstructor():参数:指明构造器的参数列表
    */
   Constructor declaredConstructor = clazz.getDeclaredConstructor(String.class);
   //2.保证此构造器是可访问的
   declaredConstructor.setAccessible(true);

   //3.调用此构造器创建运行时类的对象
   Object per =(Person) declaredConstructor.newInstance("Tom");
   System.out.println(per);
}


   public static void main(String[] args) throws Exception
  {
    //   new ReflectionTest().testField();
     // new ReflectionTest().testField1();
       //new ReflectionTest().testtMethod();
       new ReflectionTest().testConstructor();
  }
}
 复习:
  • 写出获取Class实例的三种常见方式:

                   1. Class clazz1=String.class;//不建议使用,写死了的

                   2. Class clazz2= person.getClass(); //通过对象

                   3. Class clazz3= Class.forName(String classPath);//用的比较多 ,更能体现反射的动态性

  • 对Class类的理解:

                                    Class实例对应着加载到内存中的一个运行时类。

  • 创建Class对应运行时类的对象的统用方法,代码实现。以及这样操作,需要对应的运行时类构造器方面满足需求。

                        Object obj= clazz.newInstance();//创建了对应的运行时类的对象

                                1. 必须有空参的构造器

                                2. 权限修饰符的权限要够,通常设置为public

 

  • 在工程或module的src下有名为jdbc.properties的配置文件,文件内容为:name=Tom。如何在程序中通过代码获取Tom这个变量

  • 如何调用方法show()

//类声明如下:
class User{
   public void show()
  {
       System.out.println("woshiyigezhoguoren!");
  }
}


User user=(User)clazz.newInstance();
Method show=clazz.getDeclaredMethod("show");
show.setAccessiable(true);
show.invoke(user);
 

 

posted @ 2022-08-06 11:38  zjw_rp  阅读(146)  评论(0)    收藏  举报