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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黄锦成 金牌黑马   /  2013-4-16 23:22  /  1593 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 黄锦成 于 2013-4-16 23:26 编辑

在学习老张的高新技术时,就学习了反射,反射对于设计框架的人来说很常用。一般的程序员不会很经常使用它,但是我们理解好了,有时在工作中使用到它,会使问题很快被解决。
大家在学习老张高新技术时,都会学习到。现在在黑马学习,深知反射的重要性,对于深入学习框架,理解框架原理有很大的帮助。下面我直接出题目给大家做做。大家看看能不能做,答案跟帖,有金币拿哦!


1、一个javabean为Person,有属性username,password,birthday,birthday的类型是Date,其余的是String,有一个Map,它的内容为html表单用户输入的数据,
为{username="itheima",password="it123456",birthday="1998-10-11"},通过反射创建一个Person对象,并为Person对象属性赋值。

2、通过反射,将java.util.Date类的属性、方法、构造函数都打印出来。

就先这两道题了,临时上来论坛,没想好一些题目,第二题是视频上有的,我直接用了,第一题是在上课时,涉及到了,我简化了一些,希望大家能做做!之后我会准备一些题目给大家

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1 技术分比我多。。 不给你加了 ╮(╯▽╰).

查看全部评分

4 个回复

倒序浏览
本帖最后由 杨冉 于 2013-4-18 02:02 编辑

都发了一天了也没个沙发。。占个位置好了~
1.

  1. import java.beans.PropertyDescriptor;
  2. import java.lang.reflect.Method;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import java.util.HashMap;

  6. public class Test {
  7. public static void main(String[] args) throws Exception {

  8. Person p = (Person) Class.forName("Person").newInstance();

  9. HashMap<String, String> m = new HashMap<String, String>();
  10. m.put("username", "itheima");
  11. m.put("password", "it123456");
  12. m.put("birthday", "1998-10-11");

  13. Util.setProperty(p, "username", m.get("username"));
  14. Util.setProperty(p, "password", m.get("password"));
  15. Util.setProperty(p, "birthday", new SimpleDateFormat("yyyy-MM-dd").parse(m.get("birthday")));

  16. System.out.println("username:" + p.getUsername());
  17. System.out.println("password:" + p.getPassword());
  18. System.out.println("birthday:" + p.getBirthday());
  19. }

  20. }

  21. class Util {

  22. public static void setProperty(Object obj, String propertyName, Object value)
  23. throws Exception {
  24. PropertyDescriptor pd = new PropertyDescriptor(propertyName, obj.getClass());
  25. Method methodSet = pd.getWriteMethod();
  26. methodSet.invoke(obj, value);
  27. }

  28. }

  29. class Person {
  30. private String username;
  31. private String password;
  32. private Date birthday;

  33. public String getUsername() {
  34. return username;
  35. }

  36. public void setUsername(String username) {
  37. this.username = username;
  38. }

  39. public String getPassword() {
  40. return password;
  41. }

  42. public void setPassword(String password) {
  43. this.password = password;
  44. }

  45. public Date getBirthday() {
  46. return birthday;
  47. }

  48. public void setBirthday(Date birthday) {
  49. this.birthday = birthday;
  50. }

  51. }
复制代码
2.
  1. import java.lang.reflect.Constructor;
  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.Method;
  4. import java.lang.reflect.Modifier;

  5. public class Test {
  6. public static void main(String[] args) throws Exception {

  7. Class clazz = Class.forName("java.util.Date");


  8. System.out.println("-----------------------------------Properties----------------------------");

  9. Field[] properties = clazz.getDeclaredFields();
  10. for (Field property : properties) {
  11. StringBuilder sb = new StringBuilder(Modifier.toString(property.getModifiers()));
  12. String type = property.getType().toString();
  13. sb.append(" ").append(type).append(" ").append(property.getName());
  14. System.out.println(sb);
  15. }

  16. System.out.println("-------------------------Methods----------------------------------------");

  17. Method[] methods = clazz.getMethods();
  18. for (Method method : methods) {
  19. String name = method.getName();
  20. StringBuilder sb = new StringBuilder(name);
  21. sb.append('(');
  22. Class[] params = method.getParameterTypes();
  23. for (Class param : params) {
  24. sb.append(param.getName());
  25. sb.append(',');
  26. }
  27. if (params != null && params.length != 0)
  28. sb.deleteCharAt(sb.length() - 1);
  29. sb.append(')');
  30. System.out.println(sb);
  31. }

  32. System.out.println("------------------------Constructors-----------------------------------");

  33. Constructor[] constructors = clazz.getConstructors();
  34. for (Constructor constructor : constructors) {
  35. String name = constructor.getName();
  36. StringBuilder sb = new StringBuilder(name);
  37. sb.append('(');
  38. Class[] params = constructor.getParameterTypes();
  39. for (Class param : params) {
  40. sb.append(param.getName());
  41. sb.append(',');
  42. }
  43. if (params != null && params.length != 0)
  44. sb.deleteCharAt(sb.length() - 1);
  45. sb.append(')');
  46. System.out.println(sb);
  47. }

  48. }
  49. }
复制代码
回复 使用道具 举报
先占个楼,编码中……
回复 使用道具 举报
本帖最后由 杜鹏飞 于 2013-4-17 22:35 编辑

1.
  1. import java.util.*;
  2. import java.sql.Date;
  3. import java.lang.reflect.*;

  4. class Person{
  5.         String username;
  6.         String password;
  7.         Date birthday;
  8.         
  9.         public Person(String username,String password,Date birthday){
  10.                 this.username = username;
  11.                 this.password = password;
  12.                 this.birthday = birthday;
  13.         }
  14. }

  15. public class P1 {
  16.         public static void main(String[] args)
  17.                 throws Exception{
  18.                
  19.                 Map<String,String> map = new TreeMap<String,String>();
  20.                 map.put("username", "itheima");
  21.                 map.put("password", "it123456");
  22.                 map.put("birthday", "1998-10-11");
  23.                
  24.                 Person p = Person.class.
  25.                         getConstructor(new Class[]{String.class,String.class,Date.class}).
  26.                         newInstance(new Object[]{map.get("username"),map.get("password"),Date.valueOf(map.get("birthday"))});
  27.                
  28.                 System.out.println(p.username + "::" + p.password + "::" + p.birthday);//测试之
  29.         }
  30. }///:~<span style="line-height: 1.5;">itheima::it123456::1998-10-11</span>
复制代码
2.
  1. import java.lang.reflect.*;
  2. import java.util.Date;

  3. public class P2 {
  4.         public static void main(String[] args) {
  5.                 System.out.println("********************Constructor********************");
  6.                
  7.                 Constructor[] constructor = Date.class.getDeclaredConstructors();
  8.                 for(int i=0;i<constructor.length;i++)
  9.                         System.out.println(constructor[i]);//测试之
  10.                
  11.                 System.out.println("********************Method********************");
  12.                
  13.                 Method[] method = Date.class.getDeclaredMethods();
  14.                 for(int i=0;i<method.length;i++)
  15.                         System.out.println(method[i]);
  16.                
  17.                 System.out.println("********************Field********************");
  18.                
  19.                 Field[] field = Date.class.getDeclaredFields();
  20.                 for(int i=0;i<field.length;i++)
  21.                         System.out.println(field[i]);
  22.                
  23.         }
  24. }
复制代码
回复 使用道具 举报
黄版主,先顶贴吧,我要睡了,明天 再来看,HTML这块我在看呢!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马