反射
反射一般用来写一些通用的框架。比如ide的自动提示。
2、工厂模式
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 }
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; } }
apple=Reflect.Apple
orange=Reflect.Orange
3.分析类文件:毕竟能得到类中的方法等等
4.访问一些不能访问的变量或属性:破解别人代码