黑马程序员技术交流社区
标题:
JavaBean的相关问题
[打印本页]
作者:
官珺伟
时间:
2014-4-20 11:56
标题:
JavaBean的相关问题
本帖最后由 官珺伟 于 2014-4-20 21:58 编辑
存在一个JavaBean,它包含以下几种可能的属性: 1:boolean/Boolean 2:int/Integer 3:String
4:double/Double 属性名未知,现在要给这些属性设置默认值,以下是要求的默认值:
String类型的默认值为 字符串 www.itheima.com int/Integer类型的默认值为100 boolean/Boolean类型的默认值为true double/Double的默认值为0.01D.
只需要设置带有getXxx/isXxx/setXxx方法的属性,非JavaBean属性不设置,请用代码实现
作者:
展展
时间:
2014-4-20 13:02
这个问题之前我做过,分享我的代码和设计思路给你吧
package com.itheima;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
/*
* 5、 存在一个JavaBean,它包含以下几种可能的属性:
1:boolean/Boolean
2:int/Integer
3:String
4:double/Double
属性名未知,现在要给这些属性设置默认值,以下是要求的默认值:
String类型的默认值为字符串 www.itheima.com
int/Integer类型的默认值为100
boolean/Boolean类型的默认值为true
double/Double的默认值为0.01D.
只需要设置带有getXxx/isXxx/setXxx方法的属性,非JavaBean属性不设置,请用代码实现
*/
/*
* 设计思路:
设计一个模拟题目要求的JavaBean类,通过反射进行对其中的未知属性名的变量赋值。
1,拿到字节码文件,建立一个空构造函数的实例对象。
2,利用字节码作为参数,Introspector.getBeanInfo(JavaBean.class)得到BeanInfo实例对象。
3,通过BeanInfo的方法,beanInfo.getPropertyDescriptors()拿到该字节码的所有属性描述。
3,通过PropertyDescriptor--属性描述实例对象,通过调用getWriteMethod()方法,修改字节码中属性的值。
4,在通过属性描述中getPropertyType()方法,定位是那种类型的成员变量,筛选出后,进行相应的修改赋值。
*/
//建立模拟JavaBean类
class JavaBean{
//成员变量
private boolean flag;
private int number;
private String itcast;
private double money;
//成员变量相应的set和get方法
public void setItcast(String newitcast){
itcast=newitcast;
}
public String getItcast(){
return itcast;
}
public void setNumber(int newnumber){
number=newnumber;
}
public int getNumber(){
return number;
}
public void setFlag(boolean newflag){
flag=newflag;
}
public boolean getFlag(){
return flag;
}
public void setMoney(double newmoney){
money=newmoney;
}
public double getMoney(){
return money;
}
}
public class Test5 {
public static void main(String[] args) throws Exception {
//拿到字节码
Class<JavaBean> clazz=JavaBean.class;
//拿到字节码的一个对象实例
Object obj=clazz.newInstance();
//通过字节码拿到BeanInfo实例对象
BeanInfo beanInfo=Introspector.getBeanInfo(clazz);
//通过BeanInfo拿到这个字节码的所有属性描述
PropertyDescriptor[] pds=beanInfo.getPropertyDescriptors();
//通过属性描述对象拿到属性对应的set方法和get方法,通过set方法进行属性赋值
for(PropertyDescriptor pd:pds){
//拿到当前属性的set方法
Method setMethod=pd.getWriteMethod();
Method getMethod=pd.getReadMethod();
//拿到当前属性的类型字节码
Class<?> properclass=pd.getPropertyType();
//通过字节码来筛选出相应的属性,再对应赋值,赋值完毕后,通过打印get方法查看赋值情况。
if(properclass==String.class){
setMethod.invoke(obj, "www.itheima.com");
System.out.println(getMethod.invoke(obj));
}else if(properclass==int.class || properclass==Integer.class){
setMethod.invoke(obj, 100);
System.out.println(getMethod.invoke(obj));
}else if(properclass==Boolean.class || properclass==boolean.class){
setMethod.invoke(obj, true);
System.out.println(getMethod.invoke(obj));
}else if(properclass==Double.class || properclass==double.class){
setMethod.invoke(obj, 0.01D);
System.out.println(getMethod.invoke(obj));
}
}
}
}
复制代码
作者:
忘川
时间:
2014-4-20 13:22
public class JavaBeanDemo
{
private boolean bool;
private String str;
private int Inter;
private double dou;
//string的初始值是null,int初始值是0,double初始值是0.0,boolean初始值是false
//getXXX方法是用于获取值的,所以在get方法里加一个判断就好了,如果是刚初始化的话,就返回你设置的默认值
public String getStr() {
if(str==null)
{
return "www.itheima.com";
}
else
{
return str;
}
}
public void setStr(String str) {
this.str = str;
}
public int getInter() {
if(Inter==0)
{
return 100;
}
else {
return Inter;
}
}
public void setInter(int inter) {
Inter = inter;
}
public double getDou() {
if(dou==0.0)
{
return 0.01;
}
else {
return dou;
}
}
public void setDou(double dou) {
this.dou = dou;
}
public boolean isBool() {
if(!bool)
{
return true;
}
else {
return bool;
}
}
public void setBool(boolean bool) {
this.bool = bool;
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2