我的抽象类不知道哪个子类调用了它的构造函数。
abstract class A {
Field field;
public A() {
this.field = Manager.add(Here i would like to add the type of subclass);
}
}
我可以将类类型作为构造函数链中的参数,但我想知道是否有不同的方法。
也许我可以使用 StackTrace 来了解子类并获取它们的类型,但我认为不应该以这种方式使用它。
非常感谢您的回答:)
最佳答案
this.getClass()
将为您提供已实例化的类。
关于java - 从抽象类引用子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39291631/