黑马程序员_第五天_高新技术之反射及其应用

 

---------------------- <a href="http://edu.csdn.net/heima" target="blank">android培训</a>、<a href="http://edu.csdn.net/heima" target="blank">java培训</a>、期待与您交流! ----------------------

        反射是我以前从来没接触过的新知识,在视频讲解中称为高新技术。乍一接触感觉很难,真正入门后感觉很有趣味,但是有的时候,仍然感觉不很适应这种用法。这是我学到的集中基本用法:
   反射基础:
   String str1 = "abc";
   Class cls1 = str1.getClass();
   Class cls2 = String.class();
   Class cls3 = Class.forName("java.lang.String");
   反射构造方法:
   Constructor constructor1 = String.class.getConstructor(StringBuffer.class);
    反射成员变量:
    ReflectPoint pt1 = new ReflectPoint(3,5);
    Field fieldY = pt1.getClass().getField("y");
    System.out.println(fieldY.get(pt1));
    数组的反射应用:
    printObject(a4);
    printObject("xyz");

    private static void printObject(Object obj){
         Class clazz = obj.getClass();
         if(clazz.isArray()){
         int len = Array.getLength(obj);
         for(int i=0;i<len;i++){
           System.out.println(Array.get(obj,i));
         }
    }else{
           System.out.println(obj);
    }
}
HashSet_ArrayList:
Collection collections = new HashSet();//new ArrayList()
ReflectPoint pt1 = new ReflectPoint(3,3);
ReflectPoint pt2 = new ReflectPoint(3,3);
collections.add(pt1);
collections.add(pt2);
collections.remove(pt1);
System.out.println(collections.size());
反射技术开发框架:
InputStream ips = new FileInputStream("config.properties");
Properties props = new Properties();
props.load(ips);
ips.close();
String className = props.getProperty("className");
Collection collections = (Collection)Class.forName(className);

类加载器:
InputStream ips = ReflectTest2.class.getClassLoader().getRecourceAsStream("cn/itcast/day1/config.properties");
InputStream ips = ReflectTest2.class.getRecourceAsStream("config.properties");
InputStream ips = ReflectTest2.class.getRecourceAsStream("cn/itcast/day1/config.properties");

      通过认真地学习反射的基本技术,我感觉我对框架有了更深的认识,为以后的框架学习打下了良好的基础。微笑

---------------------- <a href="http://edu.csdn.net/heima" target="blank">android培训</a>、<a href="http://edu.csdn.net/heima" target="blank">java培训</a>、期待与您交流! ----------------------

详细请查看:<a href="http://edu.csdn.net/heima" target="blank">http://edu.csdn.net/heima</a>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值