A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

/* 声明类Student,包含3个成员变量:name、age、score,要求可以通过 new Student("张三", 22, 95) 的方式创建对象, * 并可以通过set和get方法访问成员变量*/
这道题我是这么做的但这不服和要求,请问该怎么解决?
package exam2;

public class Test5 {
        public static void main(String[] args)
        {
                 Student stu = new Student();
                stu.show();
        }

}
class Student {
    private String name;
    private int age;
    private int score;

    public String getName() {
              return name;
             }
             public void setName(String name) {
              this.name=name;
             }
             public int age() {
                 return age;
                }
                public void setAge(int age) {
                 this.age=age;
                }
               public int score() {
                   return score;
                  }
                  public void setScore(int score) {
                   this.score=score;
                  }
              

    public void show(){
        System.out.println("Name: " + name + ", age: "+ age  + ", score:" + score);
    }

}


评分

参与人数 1技术分 +1 收起 理由
乔兵 + 1

查看全部评分

14 个回复

倒序浏览
这道题主要是考你内省方面的知识,如果写成你那,黑马入学考试也太简单了吧,下面是我写的:
  1. public class Bean5 {
  2.        
  3.         private void initialise(){
  4.                 @SuppressWarnings("rawtypes")
  5.                 Class beanClass;
  6.                 BeanInfo beanInfo;
  7.                 PropertyDescriptor[] pds;
  8.                 Type type;
  9.                 Method readMethod;
  10.                 Method writeMethod;
  11.                 Object obj;
  12.                 String name;
  13.                 try {
  14.                        
  15.                         beanClass = Class.forName("wednesday.TempBean");
  16.                         obj=beanClass.newInstance();
  17.                         beanInfo=Introspector.getBeanInfo(beanClass);
  18.                         pds=beanInfo.getPropertyDescriptors();
  19.                         for(PropertyDescriptor pd:pds){
  20.                                 /**
  21.                                  * 1.获取readMethod,获取writeMethod,获取Type
  22.                                  * (获取Method的参数类型,不必这么麻烦,直接有getPropertyType方法)
  23.                                  * 2.如果writeMethod不为空,根据参数类型赋初值
  24.                                  */
  25.                                 type=pd.getPropertyType();
  26.                                 readMethod=pd.getReadMethod();
  27.                                 writeMethod=pd.getWriteMethod();
  28.                                 name=pd.getName();
  29.                                 //所有的类都继承Object类,Object类有一个getClass方法,需要排除。
  30.                                 if("class".equals(name)){
  31.                                         continue;
  32.                                 }
  33.                                 if(writeMethod!=null){
  34.                                         if(type==int.class||type==Integer.class){
  35.                                                 writeMethod.invoke(obj, 100);
  36.                                         }
  37.                                         if(type==String.class){
  38.                                                 writeMethod.invoke(obj, "字符串");
  39.                                         }
  40.                                         if(type==boolean.class||type==Boolean.class){
  41.                                                 writeMethod.invoke(obj, true);
  42.                                         }
  43.                                         if(type==double.class||type==Double.class){
  44.                                                 writeMethod.invoke(obj, 0.01D);
  45.                                         }
  46.                                 }
  47.                                 //调试信息
  48.                                 if(readMethod!=null){
  49.                                         System.out.println(readMethod.invoke(obj));
  50.                                 }
  51.                         }//End of for
  52.                        
  53.                        
  54.                 } catch (ClassNotFoundException e) {
  55.                         e.printStackTrace();
  56.                 } catch (IntrospectionException e) {
  57.                         e.printStackTrace();
  58.                 } catch (InstantiationException e) {
  59.                         e.printStackTrace();
  60.                 } catch (IllegalAccessException e) {
  61.                         e.printStackTrace();
  62.                 } catch (IllegalArgumentException e) {
  63.                         e.printStackTrace();
  64.                 } catch (InvocationTargetException e) {
  65.                         e.printStackTrace();
  66.                 }
  67.         }
  68.         @Test
  69.         public void testInitialize(){
  70.                 initialise();
  71.         }

  72. }

  73. /**
  74. * 测试用的bean
  75. * @author yajun
  76. */
  77. class TempBean{
  78.        
  79.         private String name;
  80.         private int id;
  81.         private boolean sex;
  82.         private double score;
  83.        
  84.         public String getName() {
  85.                 return name;
  86.         }
  87.         public void setName(String name) {
  88.                 this.name = name;
  89.         }
  90.         public int getId() {
  91.                 return id;
  92.         }
  93.         public void setId(int id) {
  94.                 this.id = id;
  95.         }
  96.         public boolean isSex() {
  97.                 return sex;
  98.         }
  99.         public void setSex(boolean sex) {
  100.                 this.sex = sex;
  101.         }
  102.         public double getScore() {
  103.                 return score;
  104.         }
  105.         public void setScore(double score) {
  106.                 this.score = score;
  107.         }
  108. }
复制代码



点评

看到这代码非常之牛逼..我复制去试试.  发表于 2014-1-1 17:12

评分

参与人数 1技术分 +1 收起 理由
乔兵 + 1

查看全部评分

回复 使用道具 举报 1 0
本帖最后由 由然自美 于 2014-1-1 18:30 编辑

class Student {//定义学生类
       
        private String name;//姓名
        private int age;//年龄
        private int score;//成绩
        public Student(){};//构造无参函数
        public Student(String name,int age,int score){
                       
                            this.name =name;
                        if (age<100&&age>0) {//判断年龄
                                this.age =age;
                        }else{
                        System.out.println("年龄不符合");
                        }
                        if (score>0&&score<=150) {//判断分数
                                this.score = score;
                        }else{System.out.println("分数不符合");
                        }
                             
                }
                               
                               

        public void setName(String name){
                this.name = name;
        }
        public String getName(){
                return name;
        }
        public void setAge(int age){
             if(age>0&&age<100){
                        this.age =age;
}else{System.out.println("年龄不符合");
}
        }
        public int getAge(){
                return age;
        }
        public void setScore(int score){
if(score>0&&score<150){
                this.score = score;
}else{System.out.println("分数不符合");}
        }
        public int getScore(){
                return score;
        }
        public void show(){
        System.out.println("name:"+name+",age:"+age+",score:"+score);
        }
}

class StudentTest3{
        public static void main(String[] args)
        {
                Student s = new Student("张三",22,95);
                s.show();
                s.setName("张三");
                s.setAge(22);
                s.setScore(95);
                s.show();
}
}

点评

这不周全哦.....要加判断语句,.....  发表于 2014-1-1 17:13

评分

参与人数 1技术分 +1 收起 理由
乔兵 + 1

查看全部评分

回复 使用道具 举报
通过new Student("张三", 22, 95)知道姓名、年龄、分数是通过构造函数进行传递的。
所以:
  1. public class Test01 {
  2.     public static void main(String[] args)
  3.     {
  4.              Student stu = new Student("张三", 22, 95);
  5.              stu.setAge(25);
  6.             stu.show();
  7.     }

  8. }
  9. class Student {
  10.         private String name;
  11.     private int age;
  12.     private int score;
  13.    
  14.     Student(String name,int age,int score){
  15.             this.name = name;
  16.             this.age = age;
  17.             this.score = score;
  18.     }
  19.    
  20.         public String getName() {
  21.                 return name;
  22.         }
  23.         public void setName(String name) {
  24.                 this.name = name;
  25.         }
  26.        

  27.         public int getAge() {
  28.                 return age;
  29.         }
  30.         public void setAge(int age) {
  31.                 this.age = age;
  32.         }


  33.         public int getScore() {
  34.                 return score;
  35.         }
  36.         public void setScore(int score) {
  37.                 this.score = score;
  38.         }


  39.         public void show(){
  40.             System.out.println("Name: " + name + ", age: "+ age  + ", score:" + score);
  41.         }

  42. }
复制代码



评分

参与人数 1技术分 +1 收起 理由
乔兵 + 1

查看全部评分

回复 使用道具 举报
用带有形参的构造函数就行了,代码如下:
  1. class Student{
  2.     private String name;
  3.     private int age;
  4.     private int score;
  5.         Student(String n,int a, int s)//带形参的构造函数初始化
  6.         {
  7.                 this.name = n;
  8.                 this.age = a;
  9.                 this.score = s;
  10.         }

  11.     public void show()
  12.         {
  13.         System.out.println("Name: " + name + ", age: "+ age  + ", score:" + score);
  14.     }

  15. }
复制代码
回复 使用道具 举报
2楼复杂了,其实就是构造函数他没弄,在Student类里面添加Student(String name,int age,int score){setName(name);setAge(age);setScore(score)},就行了啊,我这个最简单最正确的啦

评分

参与人数 1技术分 +1 收起 理由
乔兵 + 1

查看全部评分

回复 使用道具 举报
在主函数里面直接调用Student s=new Student("张三,23,95"); System.out.println(s.getName());System.out.println(s.getAge());System.out.println(s.getScore());就行啦
回复 使用道具 举报
Student s=new Student("张三“,23,95); 上句双引号弄错了
回复 使用道具 举报
什么判断啊,说清楚啊
回复 使用道具 举报
daoyua 发表于 2014-1-1 17:03
2楼复杂了,其实就是构造函数他没弄,在Student类里面添加Student(String name,int age,int score){setName ...

什么判断说清楚赛
回复 使用道具 举报
由然自美 发表于 2014-1-1 16:20
class Student {
        private String name;
        private int age;

忘了.重来
回复 使用道具 举报
rehan 中级黑马 2015-2-11 09:13:38
12#
The method setAge is undefined for the Type Student......什么意思啊??
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马