我有一个类 Drills 和几个扩展它的类,例如 AddDrill。我希望能够在活动之间传递这个钻对象“家庭”,所以我使用 Parcelable。我找到了这个答案和这个例子,我创建了这个 Drill 和 AddDrill 类,我认为我做错了什么,我的实现中的每一件事都好吗?钻头:
package com.simplemathgame;
import android.os.Parcel;
import android.os.Parcelable;
public abstract class Drill implements Parcelable{
protected int firstNumber;
protected int secondNumber;
protected int answer;
protected int userAnswer;
protected String userAnswerString;
protected String symbol;
protected int minBound, maxBound;
protected int drillNumber;
public String getUserAnswerString() {
return userAnswerString;
}
public void setUserAnswerString(String userAnswerString) {
this.userAnswerString = userAnswerString;
}
Drill(int min, int max){
minBound = min;
maxBound = max;
answer = answerCalculatetion();
userAnswerString = "";
}
public Drill(Parcel in) {
readFromParcel(in);
}
public int getDrillNumber() {
return drillNumber;
}
public void setDrillNumber(int drillNumber) {
this.drillNumber = drillNumber;
}
public int getUserAnswer() {
return userAnswer;
}
public void setUserAnswer(int userAnswer) {
this.userAnswer = userAnswer;
}
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public int getMinBound() {
return minBound;
}
public void setMinBound(int minBound) {
this.minBound = minBound;
}
public int getMaxBound() {
return maxBound;
}
public void setMaxBound(int maxBound) {
this.maxBound = maxBound;
}
//getters and setters
public int getFirstNumber() {
return firstNumber;
}
public void setFirstNumber(int firstNumber) {
this.firstNumber = firstNumber;
}
public int getSecondNumber() {
return secondNumber;
}
public void setSecondNumber(int secondNumber) {
this.secondNumber = secondNumber;
}
public int getAnswer() {
return answer;
}
public void setAnswer(int answer) {
this.answer = answer;
}
protected int getRandomNumber(int high, int low){
return (int) ((Math.random( ) * (high - low + 1)) + low);
}
protected abstract int answerCalculatetion();
protected String printDrill(){
String FirstNumberS;
String SecondNumberS;
if(this.getFirstNumber() < 0){
FirstNumberS = "(" + this.getFirstNumber() + ")";
}
else{
FirstNumberS = "" + this.getFirstNumber();
}
if(this.getSecondNumber() < 0){
SecondNumberS = "(" + this.getSecondNumber() + ")";
}
else{
SecondNumberS = "" + this.getSecondNumber();
}
return (FirstNumberS + " " + this.getSymbol() + " " + SecondNumberS + " " + "=");
}
protected String printDrillAnswer(){
String FirstNumberS;
String SecondNumberS;
String answerS;
if(this.getAnswer() < 0){
answerS = "(" + this.getAnswer() + ")";
}
else{
answerS = "" + this.getAnswer();
}
if(this.getFirstNumber() < 0){
FirstNumberS = "(" + this.getFirstNumber() + ")";
}
else{
FirstNumberS = "" + this.getFirstNumber();
}
if(this.getSecondNumber() < 0){
SecondNumberS = "(" + this.getSecondNumber() + ")";
}
else{
SecondNumberS = "" + this.getSecondNumber();
}
return (FirstNumberS + " " + this.getSymbol() + " " + SecondNumberS + " " + "=" + " " + answerS);
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// We just need to write each field into the
// parcel. When we read from parcel, they
// will come back in the same order
dest.writeInt(firstNumber);
dest.writeInt(secondNumber);
dest.writeInt(answer);
dest.writeInt(userAnswer);
dest.writeString(userAnswerString);
dest.writeString(symbol);
dest.writeInt(minBound);
dest.writeInt(maxBound);
dest.writeInt(drillNumber);
}
/**
*
* Called from the constructor to create this
* object from a parcel.
*
* @param in parcel from which to re-create object
*/
protected void readFromParcel(Parcel in) {
// We just need to read back each
// field in the order that it was
// written to the parcel
firstNumber = in.readInt();
secondNumber = in.readInt();
answer = in.readInt();
userAnswer = in.readInt();
userAnswerString = in.readString();
symbol = in.readString();
minBound = in.readInt();
maxBound = in.readInt();
}
}
添加钻:
package com.simplemathgame;
import android.os.Parcel;
import android.os.Parcelable;
public class AddDrill extends Drill {
AddDrill(int min, int max) {
super(min, max);
symbol = "+";
firstNumber = getRandomNumber(min, max);
secondNumber = getRandomNumber(min, max);
answer = answerCalculatetion();
}
private AddDrill(Parcel in) {
super(in);
}
protected int answerCalculatetion() {
return (this.getFirstNumber() + this.getSecondNumber());
}
public static final Parcelable.Creator<AddDrill> CREATOR = new Parcelable.Creator<AddDrill>() {
public AddDrill createFromParcel(Parcel in) {
return new AddDrill(in);
}
public AddDrill[] newArray(int size) {
return new AddDrill[size];
}
};
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
}
谢谢