简体   繁体   English

如何获得超类的this.getClass()。getConstructor?

[英]How to get this.getClass().getConstructor of a super class?

Is there a way for me to get a super implementation directly through getContructor ? 我有办法直接通过getContructor获得super实现吗? I want to call the constructor on "this class or any superclass". 我想在“此类或任何超类”上调用构造函数。

The scenario details are that I have a base class that builds its data using reflection but the data is coming in from an external file. 场景详细信息是我有一个基类,该基类使用反射来构建其数据,但是数据是从外部文件输入的。 The external loader has a lookup that checks to see if data exists for a particular class and all of that is wrapped into ImplementedCard , below. 外部加载器有一个查询,用于检查特定类的数据是否存在,所有数据都包装在下面的ImplementedCard中。

This works fine (enough) and isn't directly related to the question aside from my needing to be able to create all of these instances starting from an ImplementedCard instance: 这个工作正常(足够),除了我需要能够从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;
    }
}

This base class is then extended like so: 然后像下面这样扩展该基类:

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 */
}

This goes on for hundreds of classes. 这持续了数百堂课。 What I want to do is pull out the constructors that do nothing but call super with the same parameters but when I do, it breaks the getConstructor call in deepCopy . 我想要做的是拉出来的是做什么,但调用构造函数super使用相同的参数,但是当我这样做,它打破了getConstructor呼叫deepCopy

For each class, you can do: 对于每个课程,您可以执行以下操作:

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

You could also put this in a loop until you get to Object.class or some other point in the class hierarchy where you'd like to break. 您也可以将其循环放置,直到到达Object.class或类层次结构中要中断的其他位置。

As @nhylated suggested, try 如@nhylated建议,尝试

this.getClass().getSuperClass()

Here is a nice explanation regarding why 是一个很好的解释,为什么

super.getClass()

behaves like it does. 表现得像它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以在调用super()之前访问this.getClass() - Is it possible to acces this.getClass() before calling super() 如何获取父基类对象super.getClass() - How to get the parent base class object super.getClass() 如何使用this.getClass()。getResource(String)? - How to use this.getClass().getResource(String)? this.getClass()。getClassLoader()和ClassLoader - this.getClass().getClassLoader() and ClassLoader 如何在泛型类中使用getConstructor? - How to use getConstructor with Generic class? “ this.getClass()。getSuperclass()”的意外输出 - Unexpected output of “this.getClass().getSuperclass()” this.getClass()。getClassLoader()。getResource()==异常 - this.getClass().getClassLoader().getResource() == Exception 为什么在Java中这是可能的:this.getClass()。getClass()。getClass()...等 - Why is this possible in Java: this.getClass().getClass().getClass()…etc 为什么this.getClass给它自己的类名而不是匿名类名? - Why does this.getClass give it's own class name rather than Anonymous class name? LoggerFactory.getLogger(ClassName.class) vs LoggerFactory.getLogger(this.getClass().getName()) - LoggerFactory.getLogger(ClassName.class) vs LoggerFactory.getLogger(this.getClass().getName())
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM