private属性,没有set方法,只有get方法,如何给这个属性赋值?
publicclassFoo{privateStringreadwrite;//withgetterandsetterprivateStringreadonly;//wi...
public class Foo {
private String readwrite; // with getter and setter
private String readonly; // with getter
public String getReadwrite() {
return readwrite;
}
public void setReadwrite(String readwrite) {
this.readwrite = readwrite;
}
public String getReadonly() {
return readonly;
}
}
当我在其他类中调用Foo时,想给其readonly赋值,怎么办?
找到一个扩展包JODD,这个可以:
Foo foo=new Foo();
BeanUtil.setDeclaredProperty(foo, "readonly", "data");
System.out.println(foo.getReadonly());
但是他是怎么做到的?很奇怪 展开
private String readwrite; // with getter and setter
private String readonly; // with getter
public String getReadwrite() {
return readwrite;
}
public void setReadwrite(String readwrite) {
this.readwrite = readwrite;
}
public String getReadonly() {
return readonly;
}
}
当我在其他类中调用Foo时,想给其readonly赋值,怎么办?
找到一个扩展包JODD,这个可以:
Foo foo=new Foo();
BeanUtil.setDeclaredProperty(foo, "readonly", "data");
System.out.println(foo.getReadonly());
但是他是怎么做到的?很奇怪 展开
展开全部
那是第三方写的一个框架,并非Java自带的,Java中确实存在可以不依靠setter来改变private的变量,但相当麻烦,如果不是专门查找我是真没听过,BeanUtils则简化了过程,看看这个网站吧:
http://blog.csdn.net/orclight/article/details/6913162
http://blog.csdn.net/orclight/article/details/6913162
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询