java - 如何获取父类(super class)的this.getClass().getConstructor?

标签 java reflection constructor

有没有办法让我直接通过getContructor获得super实现?我想调用“此类或任何父类(super class)”的构造函数。

场景详细信息是,我有一个基类,它使用反射构建其数据,但数据来自外部文件。外部加载器有一个查找功能,用于检查特定类的数据是否存在,并且所有这些都被包装到下面的 ImplementedCard 中。

这工作得很好(足够),除了我需要能够从 ImplementedCard 实例开始创建所有这些实例之外,与问题没有直接关系:

public class Card implements DeepCopyable<Card> {
   protected ImplementedCardList.ImplementedCard implementedCard;

   public Card() {
        this.implementedCard = ImplementedCardList.getInstance().getCardForClass(this.getClass());
        this.initFromImplementedCard(this.implementedCard);
    }

    public Card(ImplementedCardList.ImplementedCard implementedCard) {
        this.implementedCard = implementedCard;
        this.initFromImplementedCard(this.implementedCard);
    }

    public void initFromImplementedCard(ImplementedCardList.ImplementedCard implementedCard) {
        if (implementedCard != null) {
            this.name_ = implementedCard.name_;
            /* ... and so on */
       }
    }

    // This deepCopy pattern is required because we use the class of each card to recreate it under certain circumstances
    @Override
    public Card deepCopy() {
        Card copy = null;
        try {
            try {
                copy = this.getClass().getConstructor(ImplementedCardList.ImplementedCard.class).newInstance(this.implementedCard);
            } catch(NoSuchMethodException e) {
                if(!this.getClass().equals(TestHero.class)) {
                    log.warn(this.getClass().toString() + " is missing ImplementedCard constructor");
                }
                copy = getClass().newInstance();
            } catch(InvocationTargetException e) {
                log.error("InvocationTargetException error", e);
                copy = getClass().newInstance();
            }
        } catch(InstantiationException e) {
            log.error("instantiation error", e);
        } catch(IllegalAccessException e) {
            log.error("illegal access error", e);
        }
        if (copy == null) {
            throw new RuntimeException("unable to instantiate card.");
        }

        copy.name_ = this.name_;
        /* ... and so on */

        return copy;
    }
}

然后像这样扩展这个基类:

public class Minion extends Card implements CardEndTurnInterface, CardStartTurnInterface {
    public Minion() {
        super();
    }

    public Minion(ImplementedCardList.ImplementedCard implementedCard) {
        super(implementedCard);
    }

    @Override
    public void initFromImplementedCard(ImplementedCardList.ImplementedCard implementedCard) {
        if (implementedCard != null) {
            super.initFromImplementedCard(implementedCard);

            /* custom init goes here */
        }
    }

    /* other class details follow */
}

public abstract class Hero extends Minion implements MinionSummonedInterface {
    public Hero() {
        super();
    }

    public Hero(ImplementedCardList.ImplementedCard implementedCard) {
        super(implementedCard);
    }

    /* no custom init; other class details follow */
}

public class Hunter extends Hero {
    public Hunter() {
        super();
    }

    public Hunter(ImplementedCardList.ImplementedCard implementedCard) {
        super(implementedCard);
    }

    /* no custom init; other class details follow */
}

这持续了数百个类(class)。我想要做的是取出那些除了使用相同参数调用 super 之外什么也不做的构造函数,但是当我这样做时,它会破坏 deepCopy< 中的 getConstructor 调用.

最佳答案

对于每个类,您可以执行以下操作:

Hero h = new Hero();
Class hc = h.getClass();

// Get super class and its constructor.
Class<?> sc = hc.getSuperclass();
Constructor scConst = sc.getConstructor(ImplementedCard.class);

// Get super class's parent and its constructor.
Class<?> ssc = sc.getSuperclass();
Constructor sscConst = ssc.getConstructor(ImplementedCard.class);

您也可以将其放入循环中,直到到达 Object.class 或类层次结构中您想要中断的其他点。

关于java - 如何获取父类(super class)的this.getClass().getConstructor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27878524/

相关文章:

Java - 按下按钮时如何将新的 JLabel 和 JTextPane 添加到 JPanel

Java 8 消费者/函数 Lambda 歧义

c# - 通过接口(interface)动态创建类

c# - 如何在不知道封闭泛型类型的情况下访问泛型属性

java - 当我对依赖项进行一些 pom 更改时,Intellij 会自动下载。这个 mvn install 是否在后台运行?

java - 我们如何在vertx中的TCPEventBus和SockJSEventBus之间进行通信

c - C中的反射支持

c++ - 继承和默认构造函数的一些问题

发生 C++ 堆损坏检测错误

javascript - 如何将原型(prototype)分配给已经构建的对象?