简体   繁体   中英

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

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

could work without casting ".get(index)" to 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.

2nd you should look into Java Polymorphism

I'm pretty sure after it you'll be able to answer to your own question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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