gpt4 book ai didi

java - 获取对象调用方法

转载 作者:行者123 更新时间:2023-12-01 12:30:56 25 4
gpt4 key购买 nike

在java中,如果该方法位于另一个类中,如何在它正在调用的方法中获取调用该方法的对象?我查遍了互联网并没有解决方案。有人可以帮忙吗?

最佳答案

我能想到的唯一方法是将 this 传递给方法,

static void someMethod(Object o) {
System.out.println(o);
}
void testIt() {
someMethod(this); // <-- pass the current instance to the method.
}

关于java - 获取对象调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25926606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com