java反射给字段赋值就是给实体类的set赋值,怎么做?
这个只能获取到get方法用set方法时会报错!java.lang.reflect.Methodmethod=StudentsOnDisplay.class.getDecl...
这个只能获取到get方法用set方法时会报错!
java.lang.reflect.Method method =
StudentsOnDisplay.class.getDeclaredMethod("getWorks" + i);
// 调用方法
Object obj = method.invoke(new StudentsOnDisplay()); 展开
java.lang.reflect.Method method =
StudentsOnDisplay.class.getDeclaredMethod("getWorks" + i);
// 调用方法
Object obj = method.invoke(new StudentsOnDisplay()); 展开
展开全部
亲,以下是我写的例子,你可以参考:
import java.lang.reflect.Field;
import java.util.Arrays;
import static java.lang.System.out;
enum Tweedle { DEE, DUM }
public class Book {
public long chapters = 0;
public String[] characters = { "Alice", "White Rabbit" };
public Tweedle twin = Tweedle.DEE;
public static void main(String... args) {
Book book = new Book();
String fmt = "%6S: %-12s = %s%n";
try {
Class<?> c = book.getClass();
Field chap = c.getDeclaredField("chapters");
out.format(fmt, "before", "chapters", book.chapters);
chap.setLong(book, 12);
out.format(fmt, "after", "chapters", chap.getLong(book));
Field chars = c.getDeclaredField("characters");
out.format(fmt, "before", "characters",
Arrays.asList(book.characters));
String[] newChars = { "Queen", "King" };
chars.set(book, newChars);
out.format(fmt, "after", "characters",
Arrays.asList(book.characters));
Field t = c.getDeclaredField("twin");
out.format(fmt, "before", "twin", book.twin);
t.set(book, Tweedle.DUM);
out.format(fmt, "after", "twin", t.get(book));
// production code should handle these exceptions more gracefully
} catch (NoSuchFieldException x) {
x.printStackTrace();
} catch (IllegalAccessException x) {
x.printStackTrace();
}
}
}
输出:
BEFORE: chapters = 0
AFTER: chapters = 12
BEFORE: characters = [Alice, White Rabbit]
AFTER: characters = [Queen, King]
BEFORE: twin = DEE
AFTER: twin = DUM
如果对你有帮助的话,请采纳一下。如果有任何问题,都可以联系我!
import java.lang.reflect.Field;
import java.util.Arrays;
import static java.lang.System.out;
enum Tweedle { DEE, DUM }
public class Book {
public long chapters = 0;
public String[] characters = { "Alice", "White Rabbit" };
public Tweedle twin = Tweedle.DEE;
public static void main(String... args) {
Book book = new Book();
String fmt = "%6S: %-12s = %s%n";
try {
Class<?> c = book.getClass();
Field chap = c.getDeclaredField("chapters");
out.format(fmt, "before", "chapters", book.chapters);
chap.setLong(book, 12);
out.format(fmt, "after", "chapters", chap.getLong(book));
Field chars = c.getDeclaredField("characters");
out.format(fmt, "before", "characters",
Arrays.asList(book.characters));
String[] newChars = { "Queen", "King" };
chars.set(book, newChars);
out.format(fmt, "after", "characters",
Arrays.asList(book.characters));
Field t = c.getDeclaredField("twin");
out.format(fmt, "before", "twin", book.twin);
t.set(book, Tweedle.DUM);
out.format(fmt, "after", "twin", t.get(book));
// production code should handle these exceptions more gracefully
} catch (NoSuchFieldException x) {
x.printStackTrace();
} catch (IllegalAccessException x) {
x.printStackTrace();
}
}
}
输出:
BEFORE: chapters = 0
AFTER: chapters = 12
BEFORE: characters = [Alice, White Rabbit]
AFTER: characters = [Queen, King]
BEFORE: twin = DEE
AFTER: twin = DUM
如果对你有帮助的话,请采纳一下。如果有任何问题,都可以联系我!
追问
实体类里面的字段都是private,只能用set方法来赋值。。
追答
好的
展开全部
package com.test;
import java.lang.reflect.Field;
public class A {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
try{
Class<A> c=A.class;
Field field= c.getDeclaredField("name");//获取字段
Object obj=c.newInstance();//实例化对象
field.set(obj, "aaa");//为字段赋值
System.out.println(field.get(obj));
}catch(Exception e){
e.printStackTrace();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可否贴出报错信息?
可否把你导致报错的源码贴出来?
可否把StudentsOnDisplay类的set方法源码贴出来?
更多追问追答
追问
StudentsOnDisplay类的set方法
就是一个私有方法的set方法没有其它的操作。
报错内容提示class.getDeclaredMethod("setWorks" + i);有异常,。。
追答
私有方法的话,调用前要设置访问权限:method.setAccessible(true);
不过我很奇怪,set方法一般是对外开放的,为啥要设为私有的呢。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
java反射只能取值 不能赋值
追问
可以的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
报什么错?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询