gpt4 book ai didi

Android Parcelable 和继承

转载 作者:行者123 更新时间:2023-11-29 01:58:08 25 4
gpt4 key购买 nike

我有一个类 Drills 和几个扩展它的类,例如 AddDrill。我希望能够在 Activity 之间传递这个 Drill 对象“系列”,所以我正在使用 Parcelable。我找到了 this回答和this例如,我创建了这个 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;
}
}

谢谢

最佳答案

在您的 AddDrill 类中,您需要实现 writeToParcel() 方法以及来自 Parcel 的构造函数。您可能还需要对这两种方法都使用 @Override,但您需要进行试验以找出最终解决方案

关于Android Parcelable 和继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14046667/

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