开发者

Force parsing a from parent-type to child-type?

I have a class that have many other classes extends it. So now I use GenericPool to recycle items for better performance. But the problem is, I can't cast child class, for example:

StarDustSprite sprStarDust = (StarDustSprite) splStarDust[rndGenerator.nextInt开发者_Go百科(Parent.andStarDustR.length)].obtainPoolItem();

(StarDustSprite extends a class named Sprite, and the obtainPoolItem() function return a Sprite object).

So can I force parse it? Or I have to make 10 GenericPool class if I have 10 child-classes?

Thank you.


So can I force parse it?

No, you can't cast like that, for the same reason as you can't cast for example an Object into a String.

Or I have to make 10 GenericPool class if I have 10 child-classes?

I guess so. But I wouldn't solve it like this in the first place.

If you really want to reuse objects, I suggest you just lazily initialize a list of objects, which you can later reuse.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜