JavaBean代码
来源:互联网 发布:不用网络的导航 编辑:程序博客网 时间:2024/05/16 00:53
package cn.itcast.day1;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
public class IntroSpaceTest {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
ReflectPoint pt1=new ReflectPoint(3,5);
String properName="x";
//第一个参数properName为当作JavaBean类中属性的编程名称。
//第二个参数pt1.getClass()为目标 bean 的 Class 对象(字节码)
PropertyDescriptor pd=new PropertyDescriptor(properName,pt1.getClass());
// 获得应该用于读取属性值的方法。
Method methodGetX=pd.getReadMethod();
//从pt1对象身上获取相应属性值
Object retVal=methodGetX.invoke(pt1);
System.out.println(retVal);
//获得应该用于写入属性值的方法。
Method methodSetX=pd.getWriteMethod();
//将pt1对象身上相应属性值设置为7
methodSetX.invoke(pt1,7);
//使用工具包,简化操作代码
//获取对象属性
System.out.println(BeanUtils.getProperty(pt1, "x"));
System.out.println(pt1.getX());
//设置对象属性
BeanUtils.setProperty(pt1, "x", "9");
System.out.println(pt1.getX());
//支持属性的级联操作
BeanUtils.setProperty(pt1, "birthday.time", "111");
System.out.println(BeanUtils.getProperty(pt1,"birthday.time"));
PropertyUtils.setProperty(pt1, "x", 9);
System.out.println(PropertyUtils.getProperty(pt1, "x"));
}
}
import java.util.Date;
public class ReflectPoint {
private Date birthday = new Date();
private int x;
public int y;
public String str1 = "ball";
public String str2 = "basketball";
public String str3 = "itcast";
public ReflectPoint(int x, int y) {
super();
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final ReflectPoint other = (ReflectPoint) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
@Override
public String toString(){
return str1 + ":" + str2 + ":" + str3;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
public class IntroSpaceTest {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
// TODO Auto-generated method stub
ReflectPoint pt1=new ReflectPoint(3,5);
String properName="x";
//第一个参数properName为当作JavaBean类中属性的编程名称。
//第二个参数pt1.getClass()为目标 bean 的 Class 对象(字节码)
PropertyDescriptor pd=new PropertyDescriptor(properName,pt1.getClass());
// 获得应该用于读取属性值的方法。
Method methodGetX=pd.getReadMethod();
//从pt1对象身上获取相应属性值
Object retVal=methodGetX.invoke(pt1);
System.out.println(retVal);
//获得应该用于写入属性值的方法。
Method methodSetX=pd.getWriteMethod();
//将pt1对象身上相应属性值设置为7
methodSetX.invoke(pt1,7);
//使用工具包,简化操作代码
//获取对象属性
System.out.println(BeanUtils.getProperty(pt1, "x"));
System.out.println(pt1.getX());
//设置对象属性
BeanUtils.setProperty(pt1, "x", "9");
System.out.println(pt1.getX());
//支持属性的级联操作
BeanUtils.setProperty(pt1, "birthday.time", "111");
System.out.println(BeanUtils.getProperty(pt1,"birthday.time"));
PropertyUtils.setProperty(pt1, "x", 9);
System.out.println(PropertyUtils.getProperty(pt1, "x"));
}
}
import java.util.Date;
public class ReflectPoint {
private Date birthday = new Date();
private int x;
public int y;
public String str1 = "ball";
public String str2 = "basketball";
public String str3 = "itcast";
public ReflectPoint(int x, int y) {
super();
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final ReflectPoint other = (ReflectPoint) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
@Override
public String toString(){
return str1 + ":" + str2 + ":" + str3;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
分享一下我的学习资料!!!!
Java全套顶级视频
http://pro.net.itcast.cn/View-22-1435.aspx
DotNet全套视频
http://pro.net.itcast.cn/View-23-1435.aspx
PHP全套视频
http://pro.net.itcast.cn/View-24-1435.aspx
全世界最适合0基础学习编程的网上校园
http://pro.net.itcast.cn/View-25-1435.aspx
免费申请java全套视频光盘区域
http://pro.net.itcast.cn/View-26-1435.aspx
- JavaBean代码
- PHP 相当于JavaBean的代码
- 代码生成代码,JavaBean Optional方式加强
- [示例代码]通用JavaBean资源配置(Generic JavaBean Resources)
- 普通的JavaBean代码的快速生成
- javabean+jsp猜游戏的代码
- 使用xslt动态生成javabean的代码
- JavaBean
- JAVABEAN
- JavaBean
- javabean
- JAVABEAN
- javabean
- javabean
- JavaBean
- JavaBean
- javabean
- JavaBean
- 小区宽带(动态IP)后接无线路由器的设置
- Gerrit配置
- IIS权限设置的安全相关设置
- Ural 1346
- fstream常用方法详解
- JavaBean代码
- List of 40+ Machine Learning APIs
- 推荐《Linux C编程一站式学习》,入门利器
- oracle ebs ---po
- 一个线程头文件
- XML中嵌入二进制数据的三种方式
- JSP页面跳转大全
- 如何防止多次提交和重复刷新
- 修改MYSQL密码