为生存而奔跑

   :: 首页 :: 联系 :: 聚合  :: 管理
  271 Posts :: 0 Stories :: 58 Comments :: 0 Trackbacks

留言簿(5)

我参与的团队

搜索

  •  

积分与排名

  • 积分 - 319864
  • 排名 - 75

最新评论

阅读排行榜

评论排行榜

Class.forName(xxx.xx.xx) 返回的是一个类,作用是要求JVM查找并加载指定的类。由于类与静态代码是绑定的,因此也会执行静态代码段。但是没有实例化。例如
import java.lang.reflect.Method;
public 
class DynamicLoader {
    
/**
     
* @param args
     
*/
    public static void main(String[] args) throws Exception{
        
// TODO Auto-generated method stub
        Class toRun 
= Class.forName(args[0]);
        //toRun.newInstance();
        
//Method mainMethod = finMain(toRun);
        
//mainMethod.invoke(null, new Object[]{args});
    }
    
    private static Method finMain(Class toRun){
        Method[] methods 
=  toRun.getMethods();
        
for(Method method : methods){
            
if(method.getName().equals("main")){
                
return method;
            }
        }
        
return null;
    }
}

class Echo {

    
/**
     
* @param args
     
*/
    public static void main(String[] args) {
        
// TODO Auto-generated method stub
        
for(int i=0;i<args.length;i++){
            System.out.println(
"Echo arg"+i+" = "+args[i]);
        }
    }
}

class A{
    public A(){
        System.out.println(
"init A");
    }
    
    static{
        System.out.println(
"A static");
    }
}
输入java DynamicLoader A a1 a2 a3
输出A static

如果把
toRun.newInstance();这句话的注释去掉,输出
A static
init A

Class.forName与newInstance() 相当于把Class A=new A()分为了两步: 先加载这个类,然后再实例化
不同之处是:
Class.forName可以使代码的耦合度降低
但是newInstance只能调用无参构造函数。但是new可以调用有参构造函数


import java.lang.reflect.Method;
public class DynamicLoader {
    
/**
     * 
@param args
     
*/
    
public static void main(String[] args) throws Exception{
        
// TODO Auto-generated method stub
        Class toRun = Class.forName(args[0]);
        
//toRun.newInstance();
        
//Method mainMethod = finMain(toRun);
        
//mainMethod.invoke(null, new Object[]{args});
    }
    
    
private static Method finMain(Class toRun){
        Method[] methods 
=  toRun.getMethods();
        
for(Method method : methods){
            
if(method.getName().equals("main")){
                
return method;
            }
        }
        
return null;
    }
}

class Echo {

    
/**
     * 
@param args
     
*/
    
public static void main(String[] args) {
        
// TODO Auto-generated method stub
        for(int i=0;i<args.length;i++){
            System.out.println(
"Echo arg"+i+" = "+args[i]);
        }
    }
}

class A{
    
public A(){
        System.out.println(
"init A");
    }
    
    
static{
        System.out.println(
"A static");
    }
}

class B extends A{
    
public B(){
        System.out.println(
"init B");
    }
    
    
static{
        System.out.println(
"B static");
    }
}
输入java DynamicLoader B a1 a2 a3
输出
A static
B static



import java.lang.reflect.Method;
public class DynamicLoader {
    
/**
     * 
@param args
     
*/
    
public static void main(String[] args) throws Exception{
        
// TODO Auto-generated method stub
        Class toRun = Class.forName(args[0]);
        toRun.newInstance();
        Method mainMethod 
= finMain(toRun);
        mainMethod.invoke(
nullnew Object[]{args});
    }
    
    
private static Method finMain(Class toRun){
        Method[] methods 
=  toRun.getMethods();
        
for(Method method : methods){
            
if(method.getName().equals("main")){
                
return method;
            }
        }
        
return null;
    }
}

class Echo {

    
/**
     * 
@param args
     
*/
    
public static void main(String[] args) {
        
// TODO Auto-generated method stub
        for(int i=0;i<args.length;i++){
            System.out.println(
"Echo arg"+i+" = "+args[i]);
        }
    }
}

class A{
    
public A(){
        System.out.println(
"init A");
    }
    
    
static{
        System.out.println(
"A static");
    }
}
输入java DynamicLoader Echo a1 a2 a3
输出
Echo arg0 = Echo
Echo arg1 = a1
Echo arg2 = a2
Echo arg3 = a3


posted on 2011-02-27 23:45 baby-fly 阅读(947) 评论(0)  编辑 收藏 引用 所属分类: Java