设为首页 加入收藏

TOP

使用Java 反射,对类中成员变量赋值.将Json对像转为Java对像(二)
2014-11-24 01:40:25 来源: 作者: 【 】 浏览:5
Tags:使用 Java 反射 成员 变量 Json 转为 对像
e")) {
latitude = jObj.getDouble("latitude");
}
if (jObj.has("longitude")) {
longitude = jObj.getDouble("longitude");
}
return this;
}



public int getLevel() {
return level;
}


public void setLevel(int level) {
this.level = level;
}


public String getStatus() {
return status;
}


public void setStatus(String status) {
this.status = status;
}


public boolean isBusinessLicence() {
return businessLicence;
}


public void setBusinessLicence(boolean businessLicence) {
this.businessLicence = businessLicence;
}


public boolean isHygieneLicence() {
return hygieneLicence;
}


public void setHygieneLicence(boolean hygieneLicence) {
this.hygieneLicence = hygieneLicence;
}


public String getNote() {
return note;
}


public void setNote(String note) {
this.note = note;
}


public String getEnterprise() {
return enterprise;
}


public void setEnterprise(String enterprise) {
this.enterprise = enterprise;
}


public String getPrincipal() {
return principal;
}


public void setPrincipal(String principal) {
this.principal = principal;
}


public long getTime() {
return time;
}


public void setTime(long time) {
this.time = time;
}


public String getAddress() {
return address;
}


public void setAddress(String address) {
this.address = address;
}


public double getLatitude() {
return latitude;
}


public void setLatitude(double latitude) {
this.latitude = latitude;
}


public double getLongitude() {
return longitude;
}


public void setLongitude(double longitude) {
this.longitude = longitude;
}


boolean isMe(String category) {
this.category.equals(category);
}


public String getContent() {
return note;
}


public Drawable getIcon() {
return null;
}


String getVillage() {
return null;
}


public String getCell() {
return null;
}


public String getPhone() {
return phone;
}


public void setPhone(String phone) {
this.phone = phone;
}


// 反射代码,
final private Map methodMap = new HashMap();// 以set开始的方法的map
// 可以直接获取需要的set方法.


protected void init() {
Class< > userClass = this.getClass();// Class.forName(this.getClass()); 加载类
Method[] methods = userClass.getDeclaredMethods();// 获得类的方法集合
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().startsWith("set")) {
methodMap.put(methods[i].getName().toLowerCase(), methods[i]);


}
}


}


protected void setProperty(String property, Object v) {
Method method = methodMap.get(property.toLowerCase());

try {
method.invoke(this, v);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


}


对通上述的代码,可以明显发现,使用Java 反射带来的好处


推荐阅读:


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android 使用Eclipse编译 FBReade.. 下一篇实例 Android jni中调用Log输出调..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: