「java反射练习题」Java的反射
本篇文章给大家谈谈java反射练习题,以及Java的反射对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java题目 反射
public class StudentManager
{
private MapInteger,Student students;
public StudentManager(MapInteger,Student students)
{
this.students = students;
}
//增加一个学生的方法
public void addStudent(Student s)
{
//操作Map对象,把学生添加到该Map中
if(this.students.containsKey(s.getStuId())) {
this.students.put(s.getStuId(), s);
}
throw new RuntimeException("the student already exist");
}
//删除学生的方法
public void delStudent(int stuId)
{
//根据学生的学号,把该学生从管理系统中删除
if(this.students.containsKey(stuId)) {
throw new RuntimeException("cannot find the student");
}
this.students.remove(stuId);
}
//修改学生的方法:注意只修改学生的姓名和年龄
public void updStudent(Student s)
{
//根据传入的学生的学号进行信息的修改
if(this.students.containsKey(s.getStuId())) {
this.students.put(s.getStuId(), s);
} else {
throw new RuntimeException("cannot find the student");
}
}
//查询当前所有学生
public void queryStudent()
{
//把Map中的学生信息进行迭代,并显示
for(Integer key : this.students.keySet()) {
System.out.println(this.students.get(key));
}
}
}
Java,,,反射实验题目。。。。。。。。。。。。。。。
package com.baidu;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.apache.commons.lang3.StringUtils;
/**
* 反射实验题目
*
* @author GERRARD
*/
public class CopyPrrpertiesBean {
public static void main(String[] args) throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException,
SecurityException, IllegalArgumentException {
StudentInfo stuInfo = new StudentInfo();
// 初始化 StudentModel
StudentModel stuModel = new StudentModel();
stuModel.setAge(20);
stuModel.setName("gerrard");
stuModel.setStuid("test111");
copyProperties(stuModel, stuInfo);
System.out.println(stuInfo.getAge());
System.out.println(stuInfo.getHeight());
System.out.println(stuInfo.getName());
System.out.println(stuInfo.getStuid());
}
public static void copyProperties(Object source, Object target)
throws NoSuchMethodException, SecurityException,
IllegalAccessException, IllegalArgumentException,
InvocationTargetException {
// 获取 要拷贝属性类的所有属性
Field[] sourceField = source.getClass().getDeclaredFields();
// 获取 拷贝目标类所有属性
Field[] targetField = target.getClass().getDeclaredFields();
for (Field field : sourceField) {
for (Field tagField : targetField) {
// 如果属性对应 则 设置属性值
if (tagField.getName().equals(field.getName())) {
// 获取 源类该属性值 capitalize方法首字母大写,来自 org.apache.commons.lang3.StringUtils 包
Method method = source.getClass().getMethod(
"get" + StringUtils.capitalize(field.getName()));
Object value = method.invoke(source);
// 设置目标属性值
Method setMethod = target.getClass().getMethod(
"set" + StringUtils.capitalize(field.getName()),
tagField.getType());
setMethod.invoke(target, value);
}
}
}
}
}
class StudentInfo {
private String name;
private String stuid;
private int age;
private float height;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStuid() {
return stuid;
}
public void setStuid(String stuid) {
this.stuid = stuid;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
}
class StudentModel {
private String name;
private String stuid;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStuid() {
return stuid;
}
public void setStuid(String stuid) {
this.stuid = stuid;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
java反射实验题
程序逻辑有点错误
1. 你根据属性名,确定其get、set方法
2. 在拼接get、set方法时,你把属性名转大写
注:get、set 是 + 属性名(首字母大写),而不是全大写
在代码中给你注释出来了,如下
Field[] fields = classType.getDeclaredFields();
for (Field field : fields) {
String fieldName = field.getName();
String getMethodName = "get" + fieldName.toUpperCase(); // 这里错误
String setMethodName = "set" + fieldName.toUpperCase(); // 这里错误
Method getmethod = classType.getMethod(getMethodName, new Class[] {});
Method setmethod = classType.getMethod(setMethodName, field.getType());
Object price = getmethod.invoke(obj, new Object[] {});
System.out.println(fieldName + "," + price);
setmethod.invoke(reflectTester, new Object[] { price });
}
java反射练习题的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Java的反射、java反射练习题的信息别忘了在本站进行查找喔。
发布于:2022-12-21,除非注明,否则均为
原创文章,转载请注明出处。