Class Foo {    public void testFun() {        //我要在控制台输出正在调用的方法名,也就是"Foo.testFun"    }}

解决方案 »

  1.   

    在你注释的地方直接System.out.println("Foo.testFun")。如果你不想在每个方法里都加上这句话,但又想得到这个结果,那么你就得创建个动态代理。在你注释的地方是得不到你想要的结果的。
      

  2.   

    要求能不能再具体点……
    class Foo {
      public void testFun() {
      //我要在控制台输出正在调用的方法名,也就是"Foo.testFun"
          System.out.println(this.getClass().getSimpleName()+".testFun");
       //这样也满足了你的要求……
      }
    }
      

  3.   


    import java.lang.reflect.Method;
    Class Foo {
     public void testFun() {
                    Class clz = ThreadTest.class; Method[] methods = clz.getDeclaredMethods(); for (Method m : methods) { System.out.println(m.getName()); }
    }
    }
      

  4.   

    应该是import java.lang.reflect.Method;
    Class Foo {    
     public void testFun() {    
                    Class clz = Foo.class;        Method[] methods = clz.getDeclaredMethods();        for (Method m : methods) {            System.out.println(m.getName());        }
    }
    }
      

  5.   

    这个还用不到反射public void testFun() {
      Thread current = Thread.currentThread(); // 当前线程
      StackTraceElement[] elems = current.getStackTrace(); // 调用的堆栈信息
      // 我要在控制台输出正在调用的方法名,也就是"Foo.testFun"
      System.out.println(elems[0].getClassName() + "." + elems[0].getMethodName());
    }StackTraceElement 里面大把函数供你玩:
      String getClassName()  
      String getFileName()  
      int getLineNumber()  
      String getMethodName()
      

  6.   

    如果你要这样打印的话可以考虑用AOP的方式,具体的你可能需要自己去了解一下了。当然动态代理也是可以的,但是你就不知道对象的名称了,因为生成的是代理对象,名称都是$Proxy0这样的东西,下面可以给动态代理的方式。package org;import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;public class Test implements T{ public static void main(String[] args) {
    ((T)ObjectDynamicProxy.factory(new Test())).say();
    }
    public void say(){
    System.out.println("hello world");
    }
    }interface T{
    public void say();
    }class ObjectDynamicProxy implements InvocationHandler{ //真实对象引入
    private Object proxyObj; public void setObj(Object obj) {
    this.proxyObj = obj;
    }

    public ObjectDynamicProxy(Object obj){
    this.proxyObj = obj;
    } public Object invoke(Object object, Method method, Object[] args)
    throws Throwable {

    System.out.println(object.getClass() + "." + method.getName());

    Object obj = method.invoke(proxyObj, args);

    return obj;
    }

    /**
     * 代理对象生产工厂
     * @param obj
     * @return
     */
    public static Object factory(Object obj) {
    //获取传入对象的Class对象
    Class<?> classType = obj.getClass(); //生成代理对象并返回
    //该对象实现传入对象所实现的接口
    //生成的代理对象的所有方法都由第三个参数的invoke方法来接管和调用
    return Proxy.newProxyInstance(classType.getClassLoader(), classType
    .getInterfaces(), new ObjectDynamicProxy(obj));
    }
    }