모두:
Java에 다음과 같은 데이터 모델 개체가 있습니다.
class Model {
String name;
String email;
}
내가 원하는 것은 Javascript 객체와 같은 변수를 사용하여 설정하려는 속성을 동적으로 결정하는 것입니다.
String field = "name";
// This is what I try to achieve, not the real code.
New Model().field = "according content";
or
New Model()[field] = "according content";
감사
이 링크 에서와 같이 반사 할 수 있습니다.
이 예에서 그들은
public long chapters = 0;
그리고 그것을 얻을 / 설정할 수 있습니다
Field chap = c.getDeclaredField("chapters");
out.format(fmt, "before", "chapters", book.chapters);
chap.setLong(book, 12);
out.format(fmt, "after", "chapters", chap.getLong(book));
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다