Java Generic Object Passing
I'm playing around on GAE (I'm using Objectify) and wanted to make something like a generic method, but aint sure how to do it (and as far as my understandig goes, generics wouldn't be a solution for me). This is my setup:
public abstract class Cloud{
Key<Cloud> parent;
public Cloud(Key<开发者_开发技巧;Cloud> parent,...){
....
}
}
public class TagCloud extends Cloud{
public TagCloud(Key<Cloud> parent,...){
super(parent,...);
....
}
}
My goal is to do something like this;
Key<TagCloud> parentKey=put(new TagCloud(null,...));
Key<LadyGaggaCloud> childKey=put(new LadyGaggaCloud(parentKey,...));
Obviously, this doesn't work, since Cloud wants a generic Key of Cloud and not of TagCloud. I want to be able to pass all Keys of the Types of Clouds, that extend Cloud (Key< TagCloud>, Key< FooCloud>, Key< LadyGagaCloud>, etc.).
I thought about making the Cloud Class generic with templating, so I could do something of a Key< T extends Cloud> parent
and pass the extending Cloud (FooCloud) to it, but that wouldn't satisfy, because a TagCloud can have any type of Cloud as parent and not just one single type.
Does anybody have a clue how to accomplish this? Or is what I am trying to do in regard to GAE and NoSQL a stupid idea and should I handle the different types of Clouds in a datafield? Thank you for your help!
This may accomplish what you're describing:
public abstract class Cloud{
Key<? extends Cloud> parent;
public Cloud(Key<? extends Cloud> parent,...){
....
}
}
精彩评论