mercredi 24 juin 2015

Android generic property

I have on object, where one property should be possible go store generic objects. All this objects extend a basic class "BaseObject". But there are a lot of object that extend "BaseObject" and the property should be able to store all kind of these...

In Swift(iOS) I simply define a property as "AnyObject" like this:

class StoreObject
{
    var object: AnyObject?
    var list: Array<AnyObject>?
}

In Java, I tried it with this: (short version)

public class StoreObject
{
    // this should store one of the objects
    public Class<? extends BaseObject> object = null;

    // this should store a list(all the same type) of the objects
    public ArrayList<? extends BaseObject> list = null;
}

But this is not working :(

Example:

StoreObject store = new StoreObject();
store.object = new TextObject; // TestObject extends BaseObject

And here I get this error:

java.lang.ClassCastException: TestObject cannot be cast to java.lang.Class

Can somebody give me a hint on this?

Thanks, Urkman





Aucun commentaire:

Enregistrer un commentaire