gpt4 book ai didi

java - 将简单对象转换或转换为另一个类的对象

转载 作者:太空宇宙 更新时间:2023-11-03 11:45:36 25 4
gpt4 key购买 nike

我有一个对象 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=false, payment_term=false}

如何将这个 pObject 转换为以下类的对象

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
}

};

}

最佳答案

System.out.println(pObject.getClass().getName()); 的输出是什么

如果它是同一个 Customer 类,那么你可以像这样转换对象

Customer cust = (Customer) pObject;

关于java - 将简单对象转换或转换为另一个类的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7256393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com