根据你的描述,我猜测你是想知道在编程中,如何获取被调用的函数或方法的名称。下面是不同编程语言中的示例代码来解决这个问题:
import sys
def foo():
# 获取被调用函数的名称
caller = sys._getframe(1).f_code.co_name
print("被叫方:", caller)
def bar():
foo()
bar() # 输出:被叫方: bar
public class Main {
public static void foo() {
// 获取调用此方法的方法名称
String caller = Thread.currentThread().getStackTrace()[2].getMethodName();
System.out.println("被叫方:" + caller);
}
public static void bar() {
foo();
}
public static void main(String[] args) {
bar(); // 输出:被叫方: bar
}
}
function foo() {
// 获取调用此函数的函数名称
const caller = foo.caller.name;
console.log("被叫方:", caller);
}
function bar() {
foo();
}
bar(); // 输出:被叫方: bar
请注意,这些示例代码只是用于展示如何获取被调用的函数或方法的名称,并不是通用的解决方案。在实际应用中,可能会有更多的细节和限制,具体取决于编程语言和环境。
下一篇:被继承和EF Core困惑