欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > JAVA语言程序设计2(第六章:反射+设计模式)

JAVA语言程序设计2(第六章:反射+设计模式)

2025/2/23 14:03:10 来源:https://blog.csdn.net/weixin_41576682/article/details/144386279  浏览:    关键词:JAVA语言程序设计2(第六章:反射+设计模式)

一、反射

1. 反射(Reflection):允许在程序运行状态中,可以获取任意类中的属性和方法,并且可以操作任意对象内部的属性和方法,这种动态获取类的信息及动态操作对象的属性和方法对应的机制称为反射机制。

2. 类对象 和 类的对象(实例)

(1) 类的对象(实例):基于某个类 new 出来的对象,也称为实例对象。
(2) 类对象:类加载的产物,封装了一个类的所有信息(包、类名、父类、接口、属性、方法、构造方法等)

 3. 获取类对象的三种方式:

(1) 通过类的对象,获取类对象
        Student s = new Student();
        Class c = s.getClass();
(2) 通过类名获取类对象
        Class c = 类名 .class;
(3) 通过静态方法获取类对象
        Class c=Class.forName(“包名 . 类名 ”);
        注意:需要 权限类名:包名. 类名
public class TestClass {
        public static void main(String[] args) throws ClassNotFoundException {
                // 深入JVM 实现原理 / JVM 规范
                // 通过反射技术 获取 类对象
                Student s = new Student(); // 类的对象,实例
                Class c=s.getClass(); // 获取类对象
                System.out.println(c);
                // 第二种方式:
                Class c2=Class.forName("testflect.Student");
                System.out.println(c2);
                System.out.println(c==c2);
                Class c3 = Student.class;
                System.out.println(c3);
        }
}

 4. 反射常见的方法:

Class c = Class.forName("testflect.Student");
        // 动态 操作类中信息
        System.out.println(c.getName());// 获取类名
        System.out.println(c.getPackage().getName());// 获取包名
        System.out.println(c.getSuperclass().getName()); // 获取父类
        Class[] cs=c.getInterfaces();// 获取 实现的接口
        System.out.println("实现的接口数量为: "+cs.length);
        for(Class inter:cs){
                System.out.println(inter.getName());
        }
        Method[] ms = c.getMethods();// 获取公开方法:自定义 + 父类中
        System.out.println("Student类定义的方法数量为: "+ms.length);
        for(Method m:ms){
                System.out.println(m.getName());
        }
        System.out.println("--------------");
        Method[] mds = c.getDeclaredMethods();// 获取自定义方法,包含非公开的
        System.out.println("Student类中自定义的方法为: "+mds.length);
        for(Method m:mds){
                System.out.println(m.getName());
        }
        System.out.println("获取属性: ");
        Field[] fs = c.getDeclaredFields();// 获取自定义属性:包含非公开的
        for (Field f:fs){
                System.out.println(f.getName());
        }
通过反射技术获取实例:
// 通过类对象 获取 类的对象
--- 采用无参数的构造方法获取对象
Class c = Class.forName("testflect.Student");
Object obj = c.newInstance();// 默认采用无参数的构造方法
Student s =(Student)obj;
s.setName(" 佳明 ");
s.setAge(28);
s.setScore(99.0);
System.out.println(obj);
-- 采用有参数的构造方法获取对象
System.out.println(" 利用有参数的构造方法获取对象: ");
Constructor con=c.getConstructor(String.class,Integer.class,Double.class);
Object obj2=con.newInstance(" 杨浩 ",37,88.0);
System.out.println(obj2);
// 利用反射技术操作私有化的方法
Method md=c.getDeclaredMethod("test");
md.setAccessible(true);
md.invoke(obj2);

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词