java反射设置属性值,Java反射来设置属性

I have a class that has many settable/gettable attributes. I'd like to use reflection to set these attributes, but I have 2 questions about my implementation

Here is some stripped down code from my class

class Q {

public String question_1;

public String question_2;

public String question_3;

public String answer_1;

public String answer_2;

public String answer_3;

//etc. etc. Many String attributes

// … constructor and other stuff are omitted

// here is my method for "dynamically" setting each attribute

public void set_attribute(String a_raw_string, String my_field) {

try {

Class cls = Class.forName("com.xyz.models.Q");

Field fld = cls.getField(my_field);

fld.set(this, a_raw_string);

}

catch (Throwable e) {

System.err.println(e);

}

}

I then set various fields like this:

Q q = new Q();

q.set_attribute("abcde", "question_1");

q.set_attribute("defgh", "question_2");

// etc.

This works (i.e., the instance variables are set when I call set_attribute.

However, they only work when the instance variables are declared public. When they are declared private I get a NoSuchFieldException

QUESTION 1: Why do I get that error when the fields are private? My naive assumption is that since the set_attribute function is part of the class, it should have unfettered access to the instance variables.

QUESTION 2: I think I may be overthinking this problem (i.e., I shouldn't be using reflection to set variables in this way). Is there a more recommended approach?

The reason that I want to use reflection is because it's a pain in the ass to declare a ton of setter methods…so I'm wondering if someone has solved this annoyance in a better way.

Thanks!

解决方案

I think I may be overthinking this problem (i.e., I shouldn't be using reflection to set variables in this way)

Yep. Reflection is fairly slow and should only be used as a last resort. If this is simply to avoid having so much redundant code, consider using automatic code generation. For pure data objects, I would strongly recommend using protocol buffers; it will generate the getters / setters (you only need to declare the fields). Plus it allows for easy communication of the data between C++, Java, and Python.

If you have a class that has a lot of fields but isn't a pure data object... well

You should consider whether all the fields should be mutable. (Do you really need setters?)

Whether the fields should even be visible. (Do you need any accessors at all?)

It is often a good idea to make fields "final", initialize them in the constructor(s), and provide no access or provide limited access through an implemented interface.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值