这样就OK了,你new 一个JavaBean ,里面的属性不就有默认值了。

解决方案 »

  1.   

    写的没有问题。
    main方法中,你用
    JavaBean javaBean = new JavaBean();
    System.out.println(javaBean.getLength()); // 默认值
    javaBean.setLength(200);
    System.out.println(javaBean.getLength()); //自定义值
      

  2.   

    你new好这个bean后默认值就是你写在成员变量里的哪些。如 你可以在main方法里写JavaBean bean = new JavaBean();
    System.out.println(bean.getNet());  //打印的就是“www.itheima.com”//你也可以通过set方法来改变他的默认属性值
    bean.setNet("www.baidu.com");  //这样就把默认值设置为了www.baidu.com
    System.out.println(bean.getNet());//这样打印出来的就是 www.baidu.com 了
      

  3.   

    你那个题目是对于任何的bean来说都这么处理吧;
    可以使用反射实现;例如:import java.lang.reflect.Method;public class RefactorDemo { public static void main(String[] args) {
    JavaBean bean = new JavaBean();
    Book book = new Book();
    System.out.println(bean);
    System.out.println(book);
    try {
    defaultValues(bean);
    defaultValues(book);
    } catch (Exception e) {
    e.printStackTrace();
    }
    System.out.println(bean);
    System.out.println(book);
    }

    public static void defaultValues(Object obj) throws Exception{
    Method[] methods = obj.getClass().getDeclaredMethods();
    for(Method method : methods){
    String methodName = method.getName();
    if(methodName.startsWith("is") || methodName.startsWith("set")){
    Class[] paraArr = method.getParameterTypes();
    if(paraArr == null || paraArr.length != 1){
    continue;
    }else{
    if(paraArr[0] == String.class){
    method.invoke(obj, "www.itheima.com");
    }else if(paraArr[0] == int.class || paraArr[0] == Integer.class){
    method.invoke(obj, 100);
    }else if(paraArr[0] == Boolean.class || paraArr[0] == boolean.class){
    method.invoke(obj, true);
    }else if(paraArr[0] == Double.class || paraArr[0] == double.class){
    method.invoke(obj, 0.01D);
    }else{
    continue;
    }
    }
    }
    }
    }
    }class JavaBean{ 
    boolean live ;
        int length ;
        String net ;
        double number ;
     
        public boolean isLive() {
            return live;
        }
        public void setLive(boolean live) {
            this.live = live;
        }
        public int getLength() {
            return length;
        }
        public void setLength(int length) {
            this.length = length;
        }
        public String getNet() {
            return net;
        }
        public void setNet(String net) {
            this.net = net;
        }
        public double getNumber() {
            return number;
        }
        public void setNumber(double number) {
            this.number = number;
        }    public String toString(){
         return this.getClass().getName() + " : live = " + live + ", " +
            "length = " + length + ", " +
            "net = " + net + ", " +
            "number = " + number 
         ;
        }
    }class Book{
    int pageNumber;
    String bookNet;
    double price;
    public int getPageNumber() {
    return pageNumber;
    }
    public void setPageNumber(int pageNumber) {
    this.pageNumber = pageNumber;
    }
    public String getBookNet() {
    return bookNet;
    }
    public void setBookNet(String bookNet) {
    this.bookNet = bookNet;
    }
    public double getPrice() {
    return price;
    }
    public void setPrice(double price) {
    this.price = price;
    }

    @Override
    public String toString() {
    return this.getClass().getName() + " : pageNumber = " + pageNumber + ", " +
    "bookNet = " + bookNet + "," +
    "price = " + price ;

    }
    }
      

  4.   

    首先,针对这个要求是属性名是不确定的,但是属性的类型是确定的,所以可以使用内省,具体如下,不知道是不是楼主想要的public class Test {
    public static void main(String[] args) throws Exception {
    JavaBean javabean = new JavaBean();
    BeanInfo beaninfo = Introspector.getBeanInfo(JavaBean.class);
    PropertyDescriptor[] proties = beaninfo.getPropertyDescriptors();
    for (int i = 0; i < proties.length; i++) {
    String classtype = proties[i].getPropertyType().getSimpleName();
    if (classtype.equals("int") || classtype.equals("Integer")) {
    Method method = proties[i].getWriteMethod();
    method.invoke(javabean, 100);
    }
    if (classtype.equals("String")) {
    Method method = proties[i].getWriteMethod();
    method.invoke(javabean, "www.itheima.com");
    }
    if (classtype.equals("boolean") || classtype.equals("Boolean")) {
    Method method = proties[i].getWriteMethod();
    method.invoke(javabean, true);
    }
    if (classtype.equals("double") || classtype.equals("Double")) {
    Method method = proties[i].getWriteMethod();
    method.invoke(javabean, 0.01d);
    }
    }
    System.out.println(javabean.toString());
    }
    }