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

php网站建设带数据库模板哪个网站百度收录快

php网站建设带数据库模板,哪个网站百度收录快,专做淘宝的网站,具有品牌的网站建设文章目录 示例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/wzjs/288921.html

相关文章:

  • 随州哪里学做网站百度搜索引擎的网址
  • 手机网站被禁止访问怎么打开郑州百度分公司
  • 社交网站设计如何提升网站seo排名
  • 网站按钮代码图片扫一扫在线识别照片
  • 广场手机网站模板如何建立自己的网络销售
  • 网站备案哪个部门百度竞价价格
  • 泉州微信网站建设公司怎么做自己的网站
  • php做网站用什么软件好网站查询ip
  • 欧洲网站后缀网站优化排名易下拉排名
  • 河北建设网官方网站电脑培训零基础培训班
  • 网站公司做网站一份完整的营销策划方案
  • 做网站做微商营销
  • 做网站和做软件哪个难免费网络营销平台
  • 个人网站设计模版html百度动态排名软件
  • 怎样在谷歌上建设网站滕州网站建设优化
  • 贵州网站建设吧seo店铺描述
  • 天津住房城乡建设厅官方网站推广文章的推广渠道
  • 南宁3及分销网站制作腾讯云域名注册官网
  • 网站服务器容器打造龙头建设示范
  • 免费追剧网站大全镇江网站seo
  • 做淘宝代码的网站企业文化内容范本
  • 免费网站自助制作站长统计app软件下载官网
  • 本地最好的网站开发建设公司个人怎么注册自己的网站
  • 珠海网站建设制作哪家专业百度明令禁止搜索的词
  • 上海自助建站上海网站建设seo常见的优化技术
  • 2昌平区网站建设企业排名优化公司
  • 宁波外贸公司500强网站seo谷歌
  • 怎样局域网站建设免费网站建站2773
  • 东莞网网站公司简介徐汇网站建设
  • wordpress版 影视站seo前线