我想知道在Java中调用另一个类中的方法的对象的类型,例如, :在Java中调用方法的类的查找类型
class A{
public A(){
//..
}
public void method1{
//here I'd like to find what's the type of the object which is calling this method
}
}
class B{
public B(){
A a = new A();
a.method1();
}
}
class C{
public C(){
A a = new A();
a.method1();
}
}
这使我的心是抛出一个异常,分析堆栈跟踪的唯一的事,但是这将是一个可怕的做法 – 2013-05-14 14:39:05
这是不太可怕,如果只需要简单的例外。你不需要扔掉它。但是,您可以询问当前的线程来替代它的堆栈 – 2013-05-14 14:43:24
为什么你需要知道这个?我对你可能做的事情隐约感到恐惧,而不是通过传递某种枚举标志来完成。您将创建一些非常脆弱,紧密耦合的代码。 – 2013-05-14 15:03:08