比如一个类中有一个函数 void functionName() {
     callOtherFunction("functionName");
}有没有办法取得函数名称字符串呢?在callOtherFunction如果不通过指定的字符串传递能知道调用者是哪个函数吗?

解决方案 »

  1.   

    兄弟,google大法最好先用了后再问吧,网上很多
    http://www.itpub.net/viewthread.php?tid=959000
      

  2.   

    获得一些运行时的内部信息,包括属性和方法等,你可以参考一下反射java.lang.reflect
    我对这个也不是非常熟悉,但是,要取得函数名称是可以的。一个类中所包含的函数信息都可以获取。同时还可以与元数据结合使用。其他的,你先研究,研究明白了教给我哈~~
      

  3.   

      反射。getClass()获得类,下面用看看api吧,我背不出来。
      

  4.   

    反射得到但前对象;this.getClass().getName();
      

  5.   

    反射里面有可以通过本类的类Class 获得函数
      

  6.   

    反射是不行的,而java直接获取调用栈应该也比较困难,有个歪招:void testFun() {
      try{
         throw Exception("");
      }catch(Exception ex){
         //分析ex的信息,截取本函数的方法名
          callOtherFun(parseFunName(ex.toString()));
       }
    }
      

  7.   

    没那么麻烦,访问调用栈最简单,一行搞定:public class MethodName { public static void methodForTest() {
    test(Thread.currentThread().getStackTrace()[1].getMethodName());
    } public static void test(String s) {
    System.out.println(s);
    } public static void main(String[] args) {
    methodForTest();
    }}
      

  8.   

    Throwable t = new Throwable();
    StackTraceElement[] stes = t.getStackTrace();
    for(int i=0;i<stes.length;i++){
      System.out.println(stes[i].getClassName() + "类" + stes[i].getMethodName() + "方法 第" + stes[i].getLineNumber) +"行";
    }
      

  9.   

    弱弱的 问一下 这个办法 算不算 ?   public class Classname
    {
    public static void main(String[]arr)
    {Classname a=new Classname();
     System.out.println(a.toString());}
    public String toString(){return"Classname";}
    }