本文介绍了转换或铸造的简单对象到另一个类的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象pObject

对象pObject = someRpcCall();

我不知道 pObject

的类型

我所知道的是的System.out.println(pObject.toString())输出

  {partner_shipping_id = 12,partner_order_id = 11,USER_ID = 1,partner_invoice_id = 13,pricelist_id = 1,fiscal_position =假,payment_term = FALSE}

我怎么可以这样pObject转换为obect下面的类的

 进口android.os.Parcel;
进口android.os.Parcelable;
公共类客户实现Parcelable {    私人诠释身份证;
    私人字符串名称=;    公众客户(){
        // TODO自动生成构造函数存根
    }    / **
     *这将由MyCreator仅用于
     *
     * @参数源
     * /
    公众客户(包裹源){
        / *
         *从包裹重建
         * /
        ID = source.readInt();
        名称= source.readString();
    }    公共无效SETID(INT ID){
        this.id = ID;
    }    公共无效setname可以(字符串名称){
        this.name =名称;
    }    公众诠释的getId(){
        返回this.id;
    }    公共字符串的getName(){
        返回this.name;
    }    @覆盖
    公众诠释describeContents(){
        返回0;
    }    @覆盖
    公共无效writeToParcel(DEST包裹,INT标志){
        dest.writeInt(ID);
        dest.writeString(名);
    }    公共静态最终Parcelable.Creator CREATOR =新Parcelable.Creator(){        @覆盖
        公众客户createFromParcel(包裹源){
            返回新的客户(源);
        }        @覆盖
        公共客户[] newArray(INT大小){
            返回新的客户【尺寸】;
            // TODO自动生成方法存根
        }    };}


解决方案

请告诉我的输出的System.out.println(pObject.getClass()的getName());

如果它同客户类,那么你可以把对象像这样

客户卡斯特=(客户)pObject;

I've an object pObject

Object pObject = someRpcCall();

I don't know the type of pObject

What i know is System.out.println(pObject.toString()) outputs

{partner_shipping_id=12, partner_order_id=11, user_id=1, partner_invoice_id=13, pricelist_id=1, fiscal_position=false, payment_term=false}

How can I convert this pObject to obect of the following class

import android.os.Parcel;
import android.os.Parcelable;
public class Customer implements Parcelable {

    private int id;
    private String name = "";

    public Customer() {
        // TODO Auto-generated constructor stub
    }

    /**
     * This will be used only by the MyCreator
     * 
     * @param source
     */
    public Customer(Parcel source) {
        /*
         * Reconstruct from the Parcel
         */
        id = source.readInt();
        name = source.readString();
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(name);
    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {

        @Override
        public Customer createFromParcel(Parcel source) {
            return new Customer(source);
        }

        @Override
        public Customer[] newArray(int size) {
            return new Customer[size];
            // TODO Auto-generated method stub
        }

    };

}
解决方案

Whats the output of System.out.println(pObject.getClass().getName());

If its the same Customer class, then you could cast the object like this

Customer cust = (Customer) pObject;

这篇关于转换或铸造的简单对象到另一个类的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 18:40