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

c2c电商网站有哪些广陵区建设局网站

c2c电商网站有哪些,广陵区建设局网站,wordpress 手赚主题,电商网站建设论文文章目录 示例5. 通过反射获得类的private、 protected、 默认访问修饰符的属性值。6. 通过反射获得类的private方法。7. 通过反射实现一个工具BeanUtils, 可以将一个对象属性相同的值赋值给另一个对象 接上篇: 示例 5. 通过反射获得类的private、 pro…

文章目录

  • 示例
    • 5. 通过反射获得类的private、 protected、 默认访问修饰符的属性值。
    • 6. 通过反射获得类的private方法。
    • 7. 通过反射实现一个工具BeanUtils, 可以将一个对象属性相同的值赋值给另一个对象

接上篇:

示例

5. 通过反射获得类的private、 protected、 默认访问修饰符的属性值。

import java.lang.reflect.Field;class Student {private String privateField = "私有属性值";protected String protectedField = "受保护属性值";String defaultField = "默认访问属性值"; // 包级私有public String publicField = "公有属性值";
}
public class ReflectionExample5 {public static void main(String[] args) {Student student = new Student();try {// 获取类的 Class 对象Class<?> clazz = Student.class;// ================== 访问私有属性 ==================Field privateField = clazz.getDeclaredField("privateField");privateField.setAccessible(true); // 解除私有访问限制String privateValue = (String) privateField.get(student);System.out.println("privateField: " + privateValue);// ================== 访问受保护属性 ==================Field protectedField = clazz.getDeclaredField("protectedField");protectedField.setAccessible(true); // 无需继承关系即可访问String protectedValue = (String) protectedField.get(student);System.out.println("protectedField: " + protectedValue);// ================== 访问默认(包级私有)属性 ==================Field defaultField = clazz.getDeclaredField("defaultField");defaultField.setAccessible(true);String defaultValue = (String) defaultField.get(student);System.out.println("defaultField: " + defaultValue);// ================== 访问公有属性 ==================// 方式 1:getDeclaredField + setAccessible(强制访问)Field publicField1 = clazz.getDeclaredField("publicField");publicField1.setAccessible(true); // 即使公有也强制解除限制(非必须)String publicValue1 = (String) publicField1.get(student);System.out.println("publicField(强制访问): " + publicValue1);// 方式 2:直接通过 getField 获取(不推荐,仅用于对比)Field publicField2 = clazz.getField("publicField");String publicValue2 = (String) publicField2.get(student);System.out.println("publicField(正常访问): " + publicValue2);} catch (NoSuchFieldException e) {System.err.println("字段不存在: " + e.getMessage());} catch (IllegalAccessException e) {System.err.println("访问权限失败: " + e.getMessage());}}
}

关键操作说明

  1. getDeclaredField() 方法
    - 作用:获取类中声明的所有字段(包括 private/protected/默认/public)
    - 需要手动调用 setAccessible(true) 来突破非 public 属性的访问限制。
  2. setAccessible(true)
    - 方法:Field.setAccessible(true)
    - 意义:解除 Java 的访问控制检查,允许操作非公有字段。
    - 安全性警告:此操作会绕过 Java 的封装机制,仅建议在框架等需要深度操作时使用。
  3. 字段值与对象实例的关联
    - 对于 实例字段:必须传入具体对象实例(如 student),通过 field.get(object) 获取值。
    - 对于 静态字段:可直接传入 null,如 field.get(null)。

6. 通过反射获得类的private方法。

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;class MyClass3 {private String privateMethod(String param) {return "私有方法被调用,参数: " + param;}
}
public class ReflectionExample6 {public static void main(String[] args) {try {// 创建类的实例MyClass3 myObject = new MyClass3();// 获取Class对象Class<?> clazz = MyClass3.class;// 获取私有方法,需指定方法名和参数类型Method method = clazz.getDeclaredMethod("privateMethod", String.class);// 解除访问限制(关键步骤)method.setAccessible(true);// 调用方法,传入实例及参数String result = (String) method.invoke(myObject, "Hello");// 输出结果System.out.println("调用结果: " + result);} catch (NoSuchMethodException e) {System.err.println("方法未找到: " + e.getMessage());} catch (IllegalAccessException e) {System.err.println("非法访问: " + e.getMessage());} catch (InvocationTargetException e) {System.err.println("方法内部错误: " + e.getCause());}}
}

7. 通过反射实现一个工具BeanUtils, 可以将一个对象属性相同的值赋值给另一个对象

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;class BeanUtils {/*** 将源对象的属性值复制到目标对象(浅拷贝)* @param source 源对象* @param target 目标对象*/public static void copyProperties(Object source, Object target) {if (source == null || target == null) {throw new IllegalArgumentException("源对象和目标对象不能为 null");}// 获取源对象和目标对象的所有字段(包含父类字段)List<Field> sourceFields = getAllFields(source.getClass());List<Field> targetFields = getAllFields(target.getClass());for (Field sourceField : sourceFields) {// 查找目标对象中与源对象字段同名的字段Field targetField = findField(targetFields, sourceField.getName());if (targetField == null) continue;// 检查类型是否兼容(支持基本类型和包装类型)if (!isTypeCompatible(sourceField.getType(), targetField.getType())) continue;try {// 设置字段可访问性sourceField.setAccessible(true);targetField.setAccessible(true);// 复制值Object value = sourceField.get(source);targetField.set(target, value);} catch (IllegalAccessException e) {// 处理无法访问的异常System.err.println("字段复制失败: " + sourceField.getName() + " -> " + targetField.getName());}}}/*** 获取类及其父类的所有字段(非静态)*/private static List<Field> getAllFields(Class<?> clazz) {List<Field> fields = new java.util.ArrayList<>();while (clazz != null && clazz != Object.class) {fields.addAll(Arrays.stream(clazz.getDeclaredFields()).filter(f -> !isStatic(f)).collect(Collectors.toList()));clazz = clazz.getSuperclass();}return fields;}/*** 根据字段名从字段列表中查找字段*/private static Field findField(List<Field> fields, String fieldName) {return fields.stream().filter(f -> f.getName().equals(fieldName)).findFirst().orElse(null);}/*** 判断字段是否为静态*/private static boolean isStatic(Field field) {return java.lang.reflect.Modifier.isStatic(field.getModifiers());}/*** 判断源类型与目标类型是否兼容*/private static boolean isTypeCompatible(Class<?> sourceType, Class<?> targetType) {// 处理基本类型与包装类型的兼容(例如 int -> Integer)if (sourceType.isPrimitive()) {return targetType.isPrimitive() ? sourceType.equals(targetType) : getWrapperType(sourceType).equals(targetType);} else if (targetType.isPrimitive()) {return getWrapperType(targetType).equals(sourceType);} else {return targetType.isAssignableFrom(sourceType);}}/*** 获取基本类型对应的包装类型*/private static Class<?> getWrapperType(Class<?> primitiveType) {if (primitiveType == int.class) return Integer.class;if (primitiveType == long.class) return Long.class;if (primitiveType == boolean.class) return Boolean.class;if (primitiveType == byte.class) return Byte.class;if (primitiveType == char.class) return Character.class;if (primitiveType == short.class) return Short.class;if (primitiveType == double.class) return Double.class;if (primitiveType == float.class) return Float.class;return primitiveType;}
}// 父类
class Person {protected String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}// 子类
class Employee extends Person {private double salary;private Integer departmentId;public double getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}public Integer getDepartmentId() {return departmentId;}public void setDepartmentId(Integer departmentId) {this.departmentId = departmentId;}
}// 目标类
class EmployeeDTO {private String name;private int age;        // 基本类型private Double salary;  // 包装类型private Integer departmentId;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Double getSalary() {return salary;}public void setSalary(Double salary) {this.salary = salary;}public Integer getDepartmentId() {return departmentId;}public void setDepartmentId(Integer departmentId) {this.departmentId = departmentId;}
}public class ReflectionExample7 {public static void main(String[] args) {// 准备数据Employee emp = new Employee();emp.name = "张三";emp.setAge(30);emp.setSalary(15000.5);emp.setDepartmentId(101);// 目标对象EmployeeDTO dto = new EmployeeDTO();// 复制属性BeanUtils.copyProperties(emp, dto);// 验证结果System.out.println("DTO.name: " + dto.getName());System.out.println("DTO.age: " + dto.getAge());System.out.println("DTO.salary: " + dto.getSalary());System.out.println("DTO.departmentId: " + dto.getDepartmentId());}
}
http://www.dtcms.com/a/501561.html

相关文章:

  • 教育培训机构招生网站建设网站设计学习
  • 服装商城的网站策划书东莞网站制作公司怎么选择
  • 关于python项目虚拟环境创建和管理的汇总
  • 那些网站可以上传自己做的视频购物网站如何做推广
  • 广告网站设计平面设计班培训入门
  • 惠州市网站制作有限公司网页设计宣传海报素材
  • app开发网站开发洛阳小程序开发
  • 怎样做网站平台赚钱网站的黄金看盘软件
  • 网站建设百度推广开户网络推广外包一年多少钱
  • 网站设计服务表电子商务网站建设的意义是什么意思
  • 网站添加属性成都旅游景点排名前十名
  • 基于搜索引擎的网站推广方式精神文明建设网站
  • 直播网站开发费用百度seo优化多少钱
  • 网站系统建设的目标网站域名禁止续费
  • 阿里备案成功后怎么做网站佛山网站建设专业主修课程
  • 做搜狗pc网站软件太原seo网络优化招聘网
  • 如何判断网站做的关键词用哪个语言做网站比较好
  • 中山皇冠建设开发有限公司网站网站搭建的流程及费用是多少?
  • 网站内容优化的重要性哪些网站做的好看的
  • asp网站怎么安装服务器 免费
  • com0com+hub4com虚拟串口对,读写com15口的同时能够做到监听此端口
  • 电子商务网站建设流程品牌宝网站认证
  • 用上海注册的公司建的网站wordpress 报名 插件
  • 郑州网站制作网页wordpress 外企模板
  • 淘宝客网站一定要备案吗网站设计与制作的流程
  • 宁乡网站建设uuv9微信小程序开发费用一览表
  • 为什么.NET的System.IO.Compression无法解压zlib流,报错:System.IO.InvalidDataException
  • 昆明住房和城乡建设局网站免费的网页空间
  • 天河网站建设服务快速搭建网站的软件
  • 嘉鱼网站建设多少钱wordpress置顶文章 调用