Class c = this.getClass();

解决方案 »

  1.   

    但要求是在一个static函数里面得到当前的类。
    this不能用在static函数里面呀。
      

  2.   

    Class c = Class.forName(classname);//classname 类名字符窜
      

  3.   

    Class c = Class.forName(classname);
      

  4.   

    public class MyClass
    {
        public static MyMethod()
        {
            MyClass.class;
        }
    }
      

  5.   

    以上的方法我已经考虑过了。
    但事先并不知道是哪个类(子类)调用那个static函数的.比如说A有个static函数Q,
    A有子类B。那如果Q()函数被调用了,怎么分辨出是A.Q()还是B.Q()呢?
      

  6.   

    我测试了。。你可以这样
    you have a class A and create a static attribute, public static String CLASSSTRING="A".
    then your class B have a same static attribute,public static String CLASSSTRING="B".in your method Q(). you can print the CLASSSTRING. System.out.println(CLASSSTRING);如果调用的是A.Q().显示A,  调用B.Q()显示Bpublic class TestStart
    {
    public static String string = "TestStart";

    public static void main(String[] args)
    {
    TestStart.a();
    testsss.a();
    }
    public static void a()
    {
    System.out.println(string);
    }
    }class testsss extends TestStart
    {
    public static String string = "Testssss";
    public static void a()
    {
    System.out.println(string);
    }
    }
      

  7.   

    class Base{
    static String s = "Base";
    public static void a(){
    try{
    Class classname = Class.forName(s);
    System.out.println(classname);
    } catch(ClassNotFoundException e){
    System.err.println("Error");
    }
    }
    }class Derive extends Base{
    static String s ="Derive";
    }public class GetClassName{
    public static void main(String[] args){
    Base.a();
    Derive.a();
    }
    }输出:
    class Base
    class Base
      

  8.   

    如果是static方法,那么你说的“当前的类”没有意义。