[英]Refering to subclass from abstract class
i have a problem with my abstract class not knowing, which subclass calls its constructor. 我的抽象类不知道有问题,哪个子类调用了它的构造函数。
abstract class A {
Field field;
public A() {
this.field = Manager.add(Here i would like to add the type of subclass);
}
}
I could carry the class-type as a parameter in a chain of constructors, but i was wondering if there is a different way. 我可以将class-type作为参数传递给构造函数链,但是我想知道是否还有其他方法。
Maybe I could use the StackTrace to find out about the subclasses and get their type, but i don't think it should be used this way. 也许我可以使用StackTrace找出有关子类并获取其类型,但是我不认为应该以这种方式使用它。
Thank you very much in advance for your answers :) 预先非常感谢您的回答:)
this.getClass()
将为您提供实例化的类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.