首先创建一个Person类:

    1. public class Person {
    2. private String name;
    3. public int age;
    4. public Person(String name, int age) {
    5. this.name = name;
    6. this.age = age;
    7. }
    8. private Person(String name) {
    9. this.name = name;
    10. }
    11. public String getName() {
    12. return name;
    13. }
    14. public void setName(String name) {
    15. this.name = name;
    16. }
    17. public int getAge() {
    18. return age;
    19. }
    20. public void setAge(int age) {
    21. this.age = age;
    22. }
    23. public Person() {
    24. this.name = name;
    25. this.age = age;
    26. }
    27. public void show (){
    28. System.out.println("一个人");
    29. }
    30. private String showNation(String nation){
    31. System.out.println("我的国籍是:"+nation);
    32. return nation;
    33. }
    34. @Override
    35. public String
    36. toString() {
    37. return "Person{" +
    38. "name='" + name + '\'' +
    39. ", age=" + age +
    40. '}';
    41. }

    反射之前,对于Person的操作

    1. //反射之前对Person对象的操作
    2. @Test
    3. public void test1(){
    4. //1.创建Person对象
    5. Person person = new Person("喜洋洋",10);
    6. //2.通过对象,调用其内部的属性、方法
    7. person.age=10;
    8. System.out.println(person.toString());
    9. person.show();
    10. //在Person类外部,不可以通过Person类的对象调用其内部私有结构。
    11. //比如:name、showNation()以及私有的构造器
    12. }

    反射之后对于Person的操作
    1.通过反射创建Person类的对象

    1. Class personClass = Person.class;
    2. //1.通过反射创建Person类的对象
    3. Constructor constructor = personClass.getConstructor(String.class, int.class);
    4. Person tom = constructor.newInstance("Tom", 12);
    5. System.out.println(tom.toString());
    6. 2.通过反射调用指定的属性和方法
    7. Class personClass = Person.class;
    8. //1.通过反射创建Person类的对象
    9. Constructor constructor = personClass.getConstructor(String.class, int.class);
    10. Person tom = constructor.newInstance("Tom", 12);
    11. System.out.println(tom.toString());
    12. //2.通过反射调用指定的属性和方法
    13. //调用属性
    14. Field age = personClass.getDeclaredField("age");
    15. age.set(tom,10);
    16. System.out.println(tom.toString());
    17. //调用方法
    18. Method show =personClass.getDeclaredMethod("show");
    19. show.invoke(tom);
    20. 3.通过反射,可以调用Person类的私有结构的构造。比如:私有的构造器,方法属性
    21. //通过反射,可以调用Person类的私有结构的构造。比如:私有的构造器,方法属性
    22. //调用私有的构造器
    23. Constructor constructor1 = personClass.getDeclaredConstructor(String.class);
    24. constructor1.setAccessible(true);//关闭检查模式可以private的方法
    25. Person jerry = constructor1.newInstance("Jerry");
    26. System.out.println(jerry);
    27. //调用私有的属性
    28. Field name = personClass.getDeclaredField("name");
    29. name.setAccessible(true);//关闭检查模式可以private的属性
    30. name.set(jerry,"Hanmeimei");
    31. System.out.println(jerry);
    32. //调用私有的方法
    33. Method showNation = personClass.getDeclaredMethod("showNation", String.class);
    34. showNation.setAccessible(true);//关闭检查模式可以private方法
    35. showNation.invoke(jerry,"中国");//相当于String nation = p1.showNation("中国")
    36. System.out.println(showNation);

    疑问1:通过直接new的方式或反射的方式都可以调用公共的结构,开发中到底用那个?
    建议:new的方式
    什么时候会使用:反射的方式。 反射的特征:动态性
    疑问2:反射机制与面向对象中的封装性是不是矛盾的?如何看待两个技术?
    不矛盾。
    关于java.lang.Class类的理解 1.类的加载过程: 程序经过javac.exe命令以后,会生成一个或多个字节码文件(.class结尾)。 接着我们使用java.exe命令对某个字节码文件进行解释运行。相当于将某个字节码文件 加载到内存中。此过程就称为类的加载。加载到内存中的类,我们就称为运行时类,此 运行时类,就作为Class的一个实例。

    2.换句话说,Class的实例就对应着一个运行时类。 3.加载到内存中的运行时类,会缓存一定的时间。在此时间之内,我们可以通过不同的方式 来获取此运行时类。

    获取Class的实例的方式(前三种方式需要掌握)

    1. @Test
    2. public void test3() throws ClassNotFoundException {
    3. //方式一:调用运行时类的属性:class
    4. Class personClass = Person.class;
    5. System.out.println(personClass);
    6. //方式二:通过运行时类的对象,调用getClass()
    7. Person person = new Person();
    8. Class<? extends Person> personClass2 = person.getClass();
    9. System.out.println(personClass2);
    10. //方式三:调用Class的静态方法:forName(String classPath)
    11. Class<?> personClass3 = Class.forName("reflection.Person");
    12. System.out.println(personClass3);
    13. System.out.println(personClass==personClass2);
    14. System.out.println(personClass==personClass3);
    15. }

    Class实例可以是哪些结构的说明:

    1. @Test
    2. public void test4(){
    3. Class c1 = Object.class;
    4. Class c2 = Comparable.class;
    5. Class c3 = String[].class;
    6. Class c4 = int[][].class;
    7. Class c5 = ElementType.class;
    8. Class c6 = Override.class;
    9. Class c7 = int.class;
    10. Class c8 = void.class;
    11. Class c9 = Class.class;
    12. int[] a = new int[10];
    13. int[] b = new int[100];
    14. Class c10 = a.getClass();
    15. Class c11 = b.getClass();
    16. // 只要数组的元素类型与维度一样,就是同一个Class
    17. System.out.println(c10 == c11);
    18. }
    19. 对于自定义类,使用系统类加载器加载:
    20. @Test
    21. public void test1(){
    22. //对于自定义类,使用系统类加载器加载
    23. ClassLoader classLoader = ClassLoaderTest.class.getClassLoader();
    24. System.out.println(classLoader);
    25. //调用系统类加载器的getParen():获取扩展类加载器
    26. ClassLoader parent = classLoader.getParent();
    27. System.out.println(parent);
    28. //调用扩展类加载器的getParen():无法获取引导类加载器
    29. //引导类加载器主要负责加载java的核心类库,无法加载自定义类的。
    30. ClassLoader parent1 = parent.getParent();
    31. System.out.println(parent1);
    32. ClassLoader classLoader3 = String.class.getClassLoader();
    33. System.out.println(classLoader3);
    34. }
    35. /*
    36. Properties:用来读取配置文件。
    37. */
    38. @Test
    39. public void test2() throws IOException {
    40. Properties pros=new Properties();
    41. //此时的文件默认在当前的model下
    42. //读取配置文件一:
    43. FileInputStream fis=new FileInputStream("jdbc.properties");
    44. // FileInputStream fis = new FileInputStream("src\jdbc1.properties");
    45. pros.load(fis);
    46. //读取配置文件的方式二:使用ClassLoder
    47. //配置文件默认识别为:当前module的src下
    48. ClassLoader classLoader = ClassLoaderTest.class.getClassLoader();
    49. InputStream resourceAsStream = classLoader.getResourceAsStream("jdbc.properties");
    50. pros.load(resourceAsStream);
    51. String user = pros.getProperty("user");
    52. String password = pros.getProperty("password");
    53. System.out.println("user = " + user + ",password = " + password);
    54. }
    55. }
    56. 通过发射创建对应的运行时类的对象
    57. newInstance():调用此方法,创建对应的运行时类的对象。内部调用了运行时类的空参的构造器。
    58. @Test
    59. public void test1() throws IllegalAccessException, InstantiationException {
    60. Class<Person> personClass = Person.class;
    61. /*
    62. newInstance():调用此方法,创建对应的运行时类的对象。内部调用了运行时类的空参的构造器。
    63. 要想此方法正常的创建运行时类的对象,要求:
    64. 1.运行时类必须提供空参的构造器
    65. 2.空参的构造器的访问权限得够。通常,设置为public。
    66. 在javabean中要求提供一个public的空参构造器。原因:
    67. 1.便于通过反射,创建运行时类的对象
    68. 2.便于子类继承此运行时类时,默认调用super()时,保证父类有此构造器
    69. */
    70. Person person = personClass.newInstance();
    71. System.out.println(person);
    72. }
    73. 体会反射的动态性
    74. /**
    75. • 创建一个指定类的对象
    76. • classPath:指定类的全名
    77. */
    78. //体会反射的动态性
    79. @Test
    80. public void test2() throws IllegalAccessException, InstantiationException, ClassNotFoundException {
    81. for (int i = 0; i <3 ; i++) {
    82. int num = new Random().nextInt(3);
    83. String classPath="";
    84. switch (num){
    85. case 0:
    86. classPath="java.util.Date";
    87. break;
    88. case 1:
    89. classPath="java.lang.Object";
    90. break;
    91. case 2:
    92. classPath="reflection.Person";
    93. break;
    94. }
    95. Object instance = getInstance(classPath);
    96. System.out.println(instance);
    97. }
    98. }
    99. // 创建一个指定类对象 classPath指定类的全名
    100. public Object getInstance(String classPath) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
    101. Class<?> name = Class.forName(classPath);
    102. return name.newInstance();
    103. }