简体   繁体   English

getClass()作为Hashmap参数

[英]getClass() as Hashmap parameter

My problem is, that I want to do something like this: 我的问题是,我想做这样的事情:

public abstract class ClassA{
    HashMap<Byte, Class<? extends ClassA> > hashMapWhichKeyIsEqualToTypeOfExtensionClass;

    public ClassA(){
        hashMapWhichKeyIsEqualToTypeOfExtensionClass = new HashMap<Byte, getClass()>();
    }
}

In other words I want HashMap's key types to be exact to the class, which is containing it, so as to 换句话说,我希望HashMap的键类型与包含它的类完全相同,以便

public class ClassB extends ClassA{
    public ClassB iAmHereJustToShowWhatDoIwantToGet(Byte index){
        return hashMapWhichKeyIsEqualToTypeOfExtensionClass.get(index);
    }
}

could work without casting ".get(index)" to ClassB. 可以在不将“ .get(index)”强制转换为ClassB的情况下工作。

Is there a way to do this? 有没有办法做到这一点?

Well, 1st you should read about Java abstract classes to understand what you can and can't do. 首先,您应该阅读有关Java抽象类的文章,以了解您可以做什么和不能做什么。

2nd you should look into Java Polymorphism 第二,您应该研究Java多态性

I'm pretty sure after it you'll be able to answer to your own question. 我很确定之后,您将能够回答自己的问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM