Reflection (Summary on February 26)


尚硅谷代码视频:day11_反射
学习时长:1天

Overview of java reflection mechanism

Understand the Class class and obtain the Class instance (important)

Class loading and the understanding of ClassLoader

Create objects of runtime class (important)

Get the complete structure of the runtime class

Adjust the specified structure of the runtime class (important) (done during development)

Application of reflection: dynamic proxy

Learning Ideas (Silicon Valley at Station B)

Before reflection, the instantiation of the class
uses reflection to achieve the same operations as above.
The power of reflection is to call the private structure of the class
. Comparison of reflection and encapsulation

两个问题

//疑问1:通过直接new的方式或反射的方式都可以调用公共的结构,开发中到底用那个?
        //建议:直接new的方式。
        //什么时候会使用:反射的方式。 反射的特征:动态性(登录或者注册)
//疑问2:反射机制与面向对象中的封装性是不是矛盾的?如何看待两个技术?
        //不矛盾。封装性(私有的用不着,可能是被公有的调用)
        //封装性:建议是否调用。反射:能否调用。

(Formally understand reflection) (Reflection is difficult to understand: it is dynamic) 1.
Understanding of the Class class
1. The class loaded into memory, we call it the runtime class, as an instance of Class (the class itself loaded into memory, Add the .calass attribute, which acts as an instance of the big Class). In the
past, objects were created through classes. Now, the class itself is also an object, an object of the big Class.

2. In other words, an instance of Class corresponds to a runtime class.

Summary (final version)
of the main APIs related to reflection

java.lang.Class: represents a class
java.lang.reflect.Method: represents the method of the class
java.lang.reflect.Field: represents the member variable
of the class java.lang.reflect.Constructor: represents the constructor of the class

属性class作为Class的实例
Class clazz = Person.class;

得到构造器
Constructor cons = clazz.getConstructor(String.class, int.class);
通过构造器造对象
Object obj = cons.newInstance("TOM", 12);
Person p = (Person) obj;
System.out.println(p.toString());

输出:Person{
    
    name='TOM', age=12}
在这里插入代码片
/*
    配置文件:
        放数据库配置信息,jdbc即数据库驱动
        (专门用于连接数据库:主机、端口号、用户名、密码)(即hostName,userName,port,password)

        第三方服务的账号信息 如:appKey , 秘钥
 */

    //AM    11:08讲配置文件
    新建一个配置文件:对应package--->new--->Resource Bundle就可以

    因为通过创建Properties对象加载配置信息(为什么配置文件以properties结尾?)
    

Insert picture description here

Get the Class object method 1

这就拿到数据库中的配置信息,可以用jdbc连接数据库
为什么把这些信息放到文件中?       方便修改:只修改一个地方,很方便
为什么讲配置文件?     为了说明第一种获取Class对象的方式是比较好的

Get the construction method in Person

//AM 11:35
    //建一个Person类
    //成员变量、构造方法、成员方法分别设置了不同的访问权限
    //用反射试一下
    此处省略Person类的定义

/*
Constructor[] getConstructors()
Constructor[] getDeclaredConstructors()
 */
package com.cskaoyan.constructer;

import java.lang.reflect.Constructor;

/*
Constructor[] getConstructors()
Constructor[] getDeclaredConstructors()
 */
public class Demo {
    
    
    public static void main(String[] args) throws ClassNotFoundException {
    
    
        // 获取字节码文件对象personCls
                //Class 类的实例表示正在运行的 Java 应用程序中的类和接口
                //建议copy reference        com.cskaoyan.domain.Person
        Class personCls = Class.forName("com.cskaoyan.domain.Person");

        // 获取构造方法
                // getConstructors()    返回一个包含某些 Constructor 对象的数组,
                // 这些对象反映此 Class 对象所表示的类的所有 公共 构造方法。
        Constructor[] constructors = personCls.getConstructors();

        System.out.println("获取所有public的构造方法------------");
        for (Constructor c : constructors) {
    
    
            System.out.println(c);
        }

        System.out.println("获取所有构造方法-------------");
                // getDeclaredConstructors()    返回 Constructor 对象的一个数组,
                // 这些对象反映此 Class 对象表示的类声明的所有构造方法。
        Constructor[] declaredConstructors = personCls.getDeclaredConstructors();
        for (Constructor c : declaredConstructors) {
    
    
            System.out.println(c);
        }

    }
}

Get member variables in Person

/*
Field[] getFields()
Field[] getDeclaredFields()
 */

Insert picture description here

Insert picture description here

Get the member method in Person

// 获取所有成员方法 Method 用来描述我们的成员方法
                // Method[] getMethods()
                //Method[] getDeclaredMethods()

Insert picture description here
Insert picture description here

获取字节码文件对象
        Class personCls = Class.forName("com.cskaoyan.domain.Person");

		System.out.println("获取所有成员方法----------");
        Method[] declaredMethods = personCls.getDeclaredMethods();
        for (Method m : declaredMethods) {
    
    
            System.out.println(m);
        }

		获取所有成员方法----------
		private void com.cskaoyan.domain.Person.eat(java.lang.String)
		public void com.cskaoyan.domain.Person.eat()
		
		System.out.println("获取单个private的成员方法--------");
        Method eatMethod2 = personCls.getDeclaredMethod("eat", String.class);
        System.out.println(eatMethod2);
		
		获取单个private的成员方法--------
		private void com.cskaoyan.domain.Person.eat(java.lang.String)
		
		

Guess you like

Origin blog.csdn.net/AC_872767407/article/details/113925130