Test t;
System.out.println(t.getClass().getName());不知道对不对,没上机试试

解决方案 »

  1.   

    faint!
    Such a freak question.
    even if it's possible (who knows, there might exists some class in jdk that can provide information on runtime stack), it's only a hacking technique.Or, to make a joke, add a class and a static variable to store the name, like
    public class StupidContext
    {
       public static String stupidName;
    }public class Test
    {
        public static void main(String[] args)
        {
            StupidContext.stupidName = "Test";
            ShowClassName.Show();
        }
    }class ShowClassName
    {
        public static void Show()
        {
            System.out.println(StupidContext.stupidName);
            ...
        }
    }ha ha ha!
      

  2.   

    Test t=new Test();
    System.out.println(t.getClass().getName());这样肯定可以了
      

  3.   

    why not System.out.println(Test.class.getName());
    or even System.out.println("Test")?
    more straight-forward.
    :->
      

  4.   

    都不对,可能我没说清楚,只能在Show()里面写代码,外面除了可以加一些import以外不能写其他的类和函数了。to ajoo(jet pig):
    假如是"Test1"调用呢?或者一个程序有两个类或多个类调用呢?你怎么办?呵呵,再想想吧。to dynku(随风来去.cn):
    程序若没有"Test"类而改成"Test1"怎么办呢?或者一个程序有两个类或多个类调用呢?题目意思是Show()可以被任何类调用都可得知调用者。
      

  5.   

    that's why I said it's "freak question".
    I was only saying that dynku's solution is no different than simply print "Test".
    not worth time to think it.
      

  6.   

    try
    {
       throw new RuntimeException("this is a bad hack!!!!!");
    }
    catch(Throwable t)
    {
       StackTraceElement[] trace = t.getStackTrace();
       System.out.println(trace[1].getClassName());
    }and it only works in jdk1.4
    What a "Wu Ren Zi Di" question!
      

  7.   

    http://www.csdn.net/expert/topic/551/551075.xml?temp=.5851557呵呵 ,里头的反射方法太麻烦,不知道有没有简单方法
      

  8.   

    看过了,他程序作用是生成一个java文件,再用com.sun.tools.javac.Main.compile编译,再执行并在执行时发生了异常。几乎没达到效果阿,为什么后面跟着左一个“高”,又一个“高”,干什么啊?这个问题我第一个想到的是产生异常时利用printStackTrace()可向标准输出流输出出错的信息,此时栈中包含了未完成的函数的信息,于是可以利用printStackTrace(PrintWriter s)将错误输出到s中然后再处理,比如输出到图形界面上什么的,程序如下:
    class ShowClassName
    {
        public static void Show()
        {
            Exception e=new Exception();
            StringWriter bo=new StringWriter();
            PrintWriter ps=new PrintWriter(bo);
            e.printStackTrace(ps);
            String s=new String(bo.getBuffer());        //下面即可处理输出的字符串了。
        }
    }
    不知道还有没有其他的方法,考试是仓促这题我没得满分,呵呵。
      

  9.   

    then you have to write a parser?
      

  10.   

    is it a professional training class? doesn't sound like a serious college one.
      

  11.   

    ajoo(jet pig)于2002-05-08 00:55:00的回复正确,给分。