数据加载中……
Java 类反射,方法反射
2008年4月21日 Edited By DingDangXiaoMa
Java 类反射,方法反射
/**
 * 测试类反射,方法反射反射的例子。
 * 
@author DingDangXiaoMa
 
*/
public class TestMethod {

    
/**
     * 不带参数的方法。被调用的方法。
     
*/
    
public void test1() {
        System.out.println(
"in the method 1 ");
    }

    
/**
     * 主调方法。由test2方法调用test1(),
     
*/
    
public void test2() {
        
try {
            Method method 
= getClass().getMethod("test1");
            method.invoke(
thisnew Object[]{});
        } 
catch (Exception ex) {
            System.out.println(
"无法找到方法。");
        }
    }

    
/**
     *带参数的被调方法。 test1
     * 打印出传递过来的参数。
     * 
@param ss
     
*/
    
public void test1(String ss) {
        System.out.println(ss);
    }

    
/**
     * 调用带参数的方法test1(String ss);
     * 参数为String 类型
     
*/
    
public void t() {
        
try {
            Method method 
= getClass().getMethod("test1"new Class[]{
                java.lang.String.
class
            });
            method.invoke(
thisnew Object[]{
                
"aa"
            });
        } 
catch (Exception _ex) {
            
// printwriter.println("Method not supported");
            System.out.println("没有找到方法。");
        }
    }

    
public static void main(String[] args) {
        TestMethod test 
= new TestMethod();
        test.t();  
//调用带参数的方法。
        test.test2();   //调用不带参数的方法。
    }

posted on 2008-04-21 09:07 叮当小马 阅读(564) 评论(0)  编辑  收藏 所属分类: JSP/JAVA


只有注册用户登录后才能发表评论。


网站导航: