Java 如何实现写操作&;以包裹方式阅读 包裹的
我有一个Java 如何实现写操作&;以包裹方式阅读 包裹的,java,android,parcelable,Java,Android,Parcelable,我有一个Player类: public class Player implements Parcelable { private String mName; // Player's name private Card mCard; // Player's current card private boolean mLifeStatus = true; // Player's life status private boolean mProtected = false; // If the Pla
Player
类:
public class Player implements Parcelable {
private String mName; // Player's name
private Card mCard; // Player's current card
private boolean mLifeStatus = true; // Player's life status
private boolean mProtected = false; // If the Player's been protected by the guard or not
private int mId; // ID of the Player
private int mCount;
/* everything below here is for implementing Parcelable */
// 99.9% of the time you can just ignore this
@Override
public int describeContents() {
return 0;
}
// write your object's data to the passed-in Parcel
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(mName);
out.writeValue(mCard);
out.writeValue(mLifeStatus);
out.writeValue(mProtected);
out.writeInt(mId);
out.writeInt(mCount);
}
// this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
public static final Parcelable.Creator<Player> CREATOR = new Parcelable.Creator<Player>() {
public Player createFromParcel(Parcel in) {
return new Player(in);
}
public Player[] newArray(int size) {
return new Player[size];
}
};
// example constructor that takes a Parcel and gives you an object populated with it's values
private Player(Parcel in) {
mName = in.readString();
mCard = in.readValue();
mLifeStatus = in.readValue(mLifeStatus);
mProtected = in.readValue(mProtected);
mId = in.readInt();
mCount = in.readInt();
}
}
公共类播放器实现可包裹{
私有字符串mName;//播放器的名称
私人卡mCard;//玩家的当前卡
私有布尔值mLifeStatus=true;//玩家的生命状态
private boolean mpprotected=false;//玩家是否受到守卫的保护
private int mId;//播放器的ID
私人国际帐户;
/*下面的所有内容都是为了实现Parcelable*/
//99.9%的时候你可以忽略这一点
@凌驾
公共int描述内容(){
返回0;
}
//将对象的数据写入传入的地块
@凌驾
公共无效写入包(包出,内部标志){
out.writeString(mName);
out.writeValue(mCard);
out.writeValue(mLifeStatus);
out.writeValue(受保护);
out.writeInt(中);
out.writeInt(mCount);
}
//这用于重新生成对象。所有地块都必须有一个实现这两种方法的创建者
public static final Parcelable.Creator=新建Parcelable.Creator(){
公共播放器createFromParcel(包裹中){
返回新玩家(中);
}
公共播放器[]新数组(整数大小){
返回新玩家[大小];
}
};
//示例构造函数,它获取地块并为您提供一个填充了其值的对象
私人玩家(包裹内){
mName=in.readString();
mCard=in.readValue();
mLifeStatus=in.readValue(mLifeStatus);
mProtected=in.readValue(mProtected);
mId=in.readInt();
mCount=in.readInt();
}
}
我试图自己填充最后一个构造函数,但我不知道如何读取布尔值和自定义类的值,就像我的卡
类一样,它是mValuemCard
的类
我试图使用它,但仍然不起作用:mCard=in.readValue(Card.class.getClassLoader)代码>
我应该如何编写这两个方法,才能使类实现它应该是什么样子的Parcelable
dest.writeByte((byte) (myBoolean ? 1 : 0));
readFromParcel:
myBoolean = in.readByte() != 0;
参考:写包裹:
dest.writeByte((byte) (myBoolean ? 1 : 0));
readFromParcel:
myBoolean = in.readByte() != 0;
ref:地块可以存储基本类型和地块对象。这意味着存储在地块中的任何内容都必须是基本类型或可打包对象
查看Player的成员数据,我看到了一堆基本类型和一个更复杂的类型:Card
要在包裹中存储卡片,必须使卡片类包裹可随身携带
或者,如果玩家类可以访问卡的内部详细信息,您可以编写代码从卡中提取基本类型并存储它们,然后在读取端,从包裹中提取基本类型并使用它们构建卡。只有当卡片足够简单,您不必担心违反封装时,这种技术才有效。一个包裹可以存储基本类型和可包裹的对象。这意味着存储在地块中的任何内容都必须是基本类型或可打包对象
查看Player的成员数据,我看到了一堆基本类型和一个更复杂的类型:Card
要在包裹中存储卡片,必须使卡片类包裹可随身携带
或者,如果玩家类可以访问卡的内部详细信息,您可以编写代码从卡中提取基本类型并存储它们,然后在读取端,从包裹中提取基本类型并使用它们构建卡。只有当卡足够简单,您不必担心违反封装时,这种技术才有效。编写卡
out.writeParcelable(mCard, flags);
mCard = (Card) in.readParcelable(Card.class.getClassLoader());
读卡片
out.writeParcelable(mCard, flags);
mCard = (Card) in.readParcelable(Card.class.getClassLoader());
写布尔值
out.writeInt(mLifeStatus ? 1 : 0);
out.writeInt(mProtected ? 1 : 0);
mLifeStatus = in.readInt() == 1;
mProtected = in.readInt() == 1;
读取布尔值
out.writeInt(mLifeStatus ? 1 : 0);
out.writeInt(mProtected ? 1 : 0);
mLifeStatus = in.readInt() == 1;
mProtected = in.readInt() == 1;
(这就是writeValue
和readValue
如何在内部为Boolean
类型工作)写入卡
out.writeParcelable(mCard, flags);
mCard = (Card) in.readParcelable(Card.class.getClassLoader());
读卡片
out.writeParcelable(mCard, flags);
mCard = (Card) in.readParcelable(Card.class.getClassLoader());
写布尔值
out.writeInt(mLifeStatus ? 1 : 0);
out.writeInt(mProtected ? 1 : 0);
mLifeStatus = in.readInt() == 1;
mProtected = in.readInt() == 1;
读取布尔值
out.writeInt(mLifeStatus ? 1 : 0);
out.writeInt(mProtected ? 1 : 0);
mLifeStatus = in.readInt() == 1;
mProtected = in.readInt() == 1;
(这就是writeValue
和readValue
如何在内部为Boolean
类型工作)是否执行Card
包裹
?如果没有,则需要执行卡
实现包裹
?如果不是,则需要请不要公然复制粘贴答案。另外,您没有完整回答问题。请不要公然复制粘贴答案。另外,你没有完全回答这个问题。