这与问题类似: Is there a way to access the variables of the calling class in a method? 但我不太能得到确切的答案。 我正在尝试做的事情:
class A{
m()
{
int a=8;
new B.m1();
}
}
class B{
m1()
{
//Print the value of a of class A (i.e 8) here
}
}
约束: A 类中的任何内容都不应更改。仅可在 B 类中进行更改! (比如将B.m1(this)放入A类等)
最佳答案
不,这是不可能的。 特别,因为 a
是一个局部变量。
如果a
是一个字段,可能可以使用一些反射/堆栈跟踪黑客来解决这个问题。
关于java - 有没有办法在被调用类的方法中访问变量/获取调用者类的分配值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30780112/