使用接口的构造函数中的设置值错误

时间:2012-11-21 09:52:08

标签: java oop interface parent-child

我得到json,我需要用GSON解析对象:

"product":{
"product_type":"assignment",
"id":717,
    "product_profile":{
    "title":"new Order from java",
    "info":"Some special info",
    "dtl_expl":true,
    "special_info":""
    }
}

“product_profile”将是不同的对象取决于我在“product_type”中得到的内容。我为每个对象创建了类 - 赋值和写作。它们是ProductType类的子代。我在Product类中实现了应该返回正确对象的接口 - 赋值或写入取决于“product_type”中的值。我的产品类

public class Product implements IProductType{
    ProductAssignment prodAss;
    ProductWriting prodWr;
    ProductType returnState;

    @SerializedName("id")
    int id;
    @SerializedName("product_type")
    String product_type;
    @SerializedName("product_profile")
    ProductType product_profile;
    public Product()
    {

    }
    public Product(int id, String product_type, ProductType product_profile)
    {
        this.id = id;
        this.product_type = product_type;
        this.product_profile = product_profile;
    }
    public int getProductId() 
    {
         return this.id;
    }
    public String getProductType() 
    {
         return this.product_type;
    }
    public ProductType getProduct() 
    {
         return this.returnObject(product_type);
    }
    public void setProductId(int id) 
    {
        this.id = id;
    }
    public void setProductTitle(String product_type) 
    {
        this.product_type = product_type;
    }
    public void setProduct(ProductType product_profile) 
    {
        this.product_profile =  this.returnObject(product_type);
    }
    @Override
    public String toString() {
        return "id=" + id + " " + "title=" + product_type
                + " " + "profile=" + product_profile  +  "}";
    }
    @Override
    public ProductType returnObject(String res) 
    {

        System.out.println("Product");
        System.out.println(product_profile);
        if (res.equals("assignment"))
        {
            this.product_profile = new ProductAssignment();
            System.out.println("I'm going to parse Assignment");
        }
        else if  (res.equals("writing"))
            this.product_profile =  new ProductWriting();

        return product_profile;

    }

}

但是当我尝试将json解析为这样的对象时:

  Gson gson = new Gson();
  Product product = gson.fromJson(res,Product.class);

我得到了这样一个产品对象:

id=447 title=assignment profile=ProductType@e49f9fa

所以“id”和“product_type”正确解析,但它不是ProdutType中的Assignment对象。

我的ProductType类:

   public class ProductType implements IProductType{
    String product;

    static ProductType productType;
    static ProductAssignment productAssignment;
    static ProductWriting productWriting;
    IProductType component;

    ProductType returnState;
    ProductAssignment prodAss;
    ProductWriting prodWr;
    public ProductType()
    {       
    }
    public ProductType(IProductType c)
    {
         component = c;
    }
    public ProductType getProductType()
    {
        return this.returnState;
    }
    public void setProductType(ProductType returnState)
    {
        this.returnState = returnState;
    }
    @Override
    public ProductType returnObject(String product_type) 
    {
        if (product_type.equals("assignment"))
        {
            this.returnState = new ProductAssignment();
            System.out.println("I'm going to parse Assignment");
        }
        else if  (product_type.equals("writing"))
            this.returnState =  new ProductWriting();

        return returnState;
    }
    @Override
    public String toString()
    {
        return returnState.getClass().getName();
    }

}

1 个答案:

答案 0 :(得分:0)

看起来很好。要获得正确的输出,您需要覆盖toString()类中的ProductType