我正在用 Java 实现一种 ORM。我正在尝试做一个仅在父类中的静态查找方法。让我进入正题:
public class DB {
public static Object find (int id) {
// i want to return anew instance of the calling subclass
}
}
public class Item extends DB {
// nothing here
}
public class Test {
public static void main () {
Item i = (Item) Item.find(2);
...
}
}
我不知道如何让 find 方法知道它的哪个继承类正在调用它,以便我可以返回正确的实例(并且可能调用正确的构造函数等)并且继承的类可以是任何东西,没有限制.
我试过stacktrace,但它只从Test跟踪到DB。
有任何想法吗?
谢谢大家!