从抽象类引用子类 [英] Refering to subclass from abstract class

查看:76
本文介绍了从抽象类引用子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的抽象类不知道有什么问题,哪个子类调用了它的构造函数.

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);
    }
}

我可以将class-type作为参数传递给构造函数链,但是我想知道是否还有其他方法.
也许我可以使用StackTrace找出子类并获取它们的类型,但是我不认为应该以这种方式使用它.

I could carry the class-type as a parameter in a chain of constructors, but i was wondering if there is a different way.
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.

非常感谢您的回答:)

推荐答案

this.getClass()将为您提供实例化的类.

this.getClass() wil give you class that was instantiated.

这篇关于从抽象类引用子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆