首页 > 其他 > 详细

反射

时间:2014-03-25 16:45:31      阅读:483      评论:0      收藏:0      [点我收藏+]

  反射一般用来写一些通用的框架。比如ide的自动提示。

2、工厂模式

bubuko.com,布布扣
 1 package Reflect;
 2  
 3 interface fruit{
 4     public abstract void eat();
 5 }
 6  
 7 class Apple implements fruit{
 8     public void eat(){
 9         System.out.println("Apple");
10     }
11 }
12  
13 class Orange implements fruit{
14     public void eat(){
15         System.out.println("Orange");
16     }
17 }
18  
19 class Factory{
20     public static fruit getInstance(String ClassName){
21         fruit f=null;
22         try{
23             f=(fruit)Class.forName(ClassName).newInstance();
24         }catch (Exception e) {
25             e.printStackTrace();
26         }
27         return f;
28     }
29 }
30 class hello{
31     public static void main(String[] a){
32         fruit f=Factory.getInstance("Reflect.Apple");
33         if(f!=null){
34             f.eat();
35         }
36     }
37 }
bubuko.com,布布扣

 

bubuko.com,布布扣
class init{
    public static Properties getPro() throws FileNotFoundException, IOException{
        Properties pro=new Properties();
        File f=new File("fruit.properties");
        if(f.exists()){
            pro.load(new FileInputStream(f));
        }else{
            pro.setProperty("apple", "Reflect.Apple");
            pro.setProperty("orange", "Reflect.Orange");
            pro.store(new FileOutputStream(f), "FRUIT CLASS");
        }
        return pro;
    }
}
 
class Factory{
    public static fruit getInstance(String ClassName){
        fruit f=null;
        try{
            f=(fruit)Class.forName(ClassName).newInstance();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return f;
    }
}
bubuko.com,布布扣
apple=Reflect.Apple
orange=Reflect.Orange

 

 

3.分析类文件:毕竟能得到类中的方法等等
4.访问一些不能访问的变量或属性:破解别人代码

反射,布布扣,bubuko.com

反射

原文:http://www.cnblogs.com/haiyun-online/p/3623166.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!