Class a
{
public static void m1()
{
System.out.println(调用类类名);
}
}
Class b
{
public void m2()
{
a.m1;
}
}
如上面的代码,一个类调用另外一个类,调用类如何获取调用类的类名!,谢谢
{
public static void m1()
{
System.out.println(调用类类名);
}
}
Class b
{
public void m2()
{
a.m1;
}
}
如上面的代码,一个类调用另外一个类,调用类如何获取调用类的类名!,谢谢
看不懂的说
==============
变量名.getClass().getName()
得到的类名,包括前面的包名的
如果我理解错了,当我没说
{
public static void m1()
{
System.out.println("a");
}
}
Class b
{
public void m2()
{
a.m1;
}
}
===============
这样不就行了,这有什么好问的。
object有一个方法getClass()
public final Class<? extends Object> getClass()
返回一个对象的运行时类。该 Class
对象是由所表示类的 static synchronized 方法锁定的对象。返回的是一个class对象,而class类有一个方法getName()
public String getName()
以 String 的形式返回此 Class 对象所表示的实体(类、接口、数
组类、基本类型或 void)名称。具体通过 对象名.getClass().getName() 就可以获得你对象(不管是接口,类还是...)的名称
先看清问题,谢谢
{
public static void m1(Object temp)
{
System.out.println(temp.getClass().getName());
}
}public class b {
/** Creates a new instance of b */
public b() {
a.m1(this);
}
public static void main(String []args)
{
new b();
}
}这只能是我想到最简单的方法了 等等有没有高人
public void test1 () {
Thread th = Thread.currentThread();
StackTraceElement[] st = th.getStackTrace();
System.out.printf("Invoker is method %s of class %s in file %s at line %s\n",
st[2].getMethodName(),
st[2].getClassName(),
st[2].getFileName(),
st[2].getLineNumber());
}
}public class Test { public void test() {
Test1 t1 = new Test1();
t1.test1();
} public static void main(String[] args) {
Test t = new Test();
t.test();
}
}
Class b
{
public void m2()
{
System.out.println(a.getClass().getName());
}
}