简体   繁体   English

子类化ParseObject子类(Android)

[英]Subclassing a ParseObject Subclass (Android)

Is it possible to subclass subclasses of ParseObject s? 是否可以ParseObject的子类? I am following the directions here . 我按照这里的指示行事 My class looks like this: 我的班级看起来像这样:

@ParseClassName("Stove")
public class Stove extends ParseObject{

  private String URL = "url";
  private String BRAND_NAME = "brand name";

  public Stove() {
    //Needed for Parse
  }

  public Stove(String url, String brandName) {
    put(URL, url);
    put(BRAND_NAME, brandName);
  }

  public String getUrl() {
    return getString(URL);
  }

  public String getBrandName() {
    return getString(BRAND_NAME);
  }
  ...
}

and I have a subclass of this that looks like 我有一个看起来像这样的子类

@ParseClassName("ElectricStove")
public class ElectricStove extends Stove{
  public ElectricStove() {
  }

  public ElectricStove(String url, String brandName) {
    super(url, brandName);
  }
  ...
}

My Application subclass is registered in AndroidManifest.xml and has this code in onCreate() : 我的Application子类在AndroidManifest.xml注册,并在onCreate()包含此代码:

ParseObject.registerSubclass(Stove.class);
ParseObject.registerSubclass(ElectricStove.class);
...
Parse.initialize(this, "<lots of letters>", "<more letters>");
ParseInstallation.getCurrentInstallation().saveInBackground();

I'm getting this exception 我得到了这个例外

Caused by: java.lang.IllegalArgumentException: You must register this ParseObject subclass before instantiating it.
at com.parse.ParseObject.<init>(ParseObject.java:363)
at com.parse.ParseObject.<init>(ParseObject.java:324)
at <package>.Stove.<init>(Stove.java:16)
at <package>.ElectricStove.<init>(ElectricStove.java:7)

which makes me wonder if I'm going about this in the wrong way or if it's perhaps simply not possible. 这让我想知道我是否会以错误的方式解决这个问题,或者是否可能根本不可能。

It's just not possible at this point, as Parse Android SDK does not support this. 目前这是不可能的,因为Parse Android SDK不支持此功能。 Rather, as a suggestion, use an identifier to specify what type of "Stove" a particular stove object is. 相反,作为建议,使用标识符来指定特定炉灶对象的“炉灶”类型。 Take this example: 举个例子:

@ParseClassName("Instrument")
public class Instrument extends ParseObject {
    public Instrument() {
    // A default constructor is required.
    }

    public InstrumentType getType() {
        return InstrumentType.parse(getString("type"));
    }
    public void setType(InstrumentType type) {
        put("type", type.toString());
    }

then use: 然后使用:

final Instrument ocarina = new Instrument();

// Since our Instruments are strongly-typed, we can provide mutators that only take
// specific types, such as Strings, ParseUsers, or enum types.
ocarina.setType(InstrumentType.WOODWIND);

This would be a work-around of sorts to allow you to perform actions on an object based on it's type. 这将是一种解决方法,允许您根据对象的类型对对象执行操作。 It's not perfect, but it may suit your needs. 它并不完美,但它可能适合您的需求。 The InstrumentType is just a class used for static constants in order to access id values InstrumentType只是用于静态常量的类,以便访问id值

Example taken from here 这里取的例子

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

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