当前位置: 首页 > article >正文

java程序语言设计-反射加设计模式

第六章:反射+设计模式 一、反射
1. 反射(Reflection):允许在程序运行状态中,可以获取任意类中的属性和方法,并且可以操作任意对象内部的属 性和方法,这种动态获取类的信息及动态操作对象的属性和方法对应的机制称为反射机制。
2. 类对象 和 类的对象(实例)
 (1) 类的对象(实例):基于某个类 new 出来的对象,也称为实例对象。
(2) 类对象:类加载的产物,封装了一个类的所有信息(包、类名、父类、接口、属性、方法、构造方法等)
 package testio2;
import java.io.*;
public class TestInputStreamReader {
    public static void main(String[] args)  {
        BufferedReader br = null;
        try {
// 1. 创建文件字节输入流对象(基础流) ->不能更改文件编码格式(GBK) FileInputStream fis = new FileInputStream("file/m.txt"); // 2. 创建桥转换流,指定编解码格式为 "GBK"
InputStreamReader isr = new InputStreamReader(fis, "GBK"); // 3. 包装过滤流:增强读操作
br = new BufferedReader(isr); // 4. 读操作
while (true) {
                String s = br.readLine();
                if (s == null) break;
                System.out.println(s);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
if(br!=null){ // 5. 关闭流
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
} }
} }
}
 
 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);


http://www.kler.cn/a/441088.html

相关文章:

  • C++ 堆栈分配的区别
  • 572. 另一棵树的子树
  • 解析“in the wild”——编程和生活中的俚语妙用
  • 软件工程概论试题三
  • MyBatis 框架:简化 Java 数据持久化的利器
  • 2007-2020年各省国内专利申请授权量数据
  • 【使用PyQt5和YOLOv11开发电脑屏幕区域的实时分类GUI】——PyQt5在Pycharm中的安装配置
  • GCNet的简述
  • 解锁报表在线设计新高度:FastReport Online Designer 2025.1 正式上线!
  • 【C#】Debug和Release的区别和使用
  • 23. 合并 K 个升序链表(java)
  • 基于Vue 3 简单自定义Table组件(乞丐版)
  • C语言刷题(2)
  • phpSpider如何应对网页结构的变化
  • OpenCV目标检测 级联分类器 C++实现
  • 力扣--LCR 158.库存管理II
  • Python与数据库Mysql连接及操作方法
  • Day41 动态规划part08
  • 【C++】模板机制
  • SSM 垃圾分类系统:科技赋能环保新篇
  • Vue Web开发(八)
  • Android 写排行榜,顶部前三
  • 字符2
  • Group FLUX - Summary Essay of the Alpha Phase Problem
  • Next.js流量教程:如何在 Next.js 中添加结构化数据以生成丰富摘要(Rich Snippets)
  • 【现代服务端架构】传统服务器 对比 Serverless