从调用对象中获取并使用 class
Get and use class from calling object
假设我们有抽象的class以下方法。
public Object Foo() {
[X] someObject;
/* some operations*/
return someObject;
}
我想要的是从方法的 class 扩展中获取和使用调用对象的 class。 [X] 代表(伪代码)调用的对象 class。如何使用调用 Foo() 的对象的 class 初始化 someObject?
编辑:
我想获得调用该方法的 class 。然后使用 class 创建它的新对象。我得到的答复是 imposible 没有参考。
简单的说是不是不能用调用方法的class代替[X]?
我不确定这是否正是您想要的,但您可以查看当前线程的 stack trace. The second item in the array will be the caller. The problem is that the class name 可能是实际 class 呼叫您的超级 class。
假设我们有抽象的class以下方法。
public Object Foo() {
[X] someObject;
/* some operations*/
return someObject;
}
我想要的是从方法的 class 扩展中获取和使用调用对象的 class。 [X] 代表(伪代码)调用的对象 class。如何使用调用 Foo() 的对象的 class 初始化 someObject?
编辑: 我想获得调用该方法的 class 。然后使用 class 创建它的新对象。我得到的答复是 imposible 没有参考。
简单的说是不是不能用调用方法的class代替[X]?
我不确定这是否正是您想要的,但您可以查看当前线程的 stack trace. The second item in the array will be the caller. The problem is that the class name 可能是实际 class 呼叫您的超级 class。