3

祝大家有美好的一天。

我有一个带有 runRandomExercise() 方法的抽象类,以及几个扩展它以添加不同类型练习的类。

我现在想选择一个随机类型的练习,所以我需要随机选择其中一个类,然后调用 runRandomExercise() 。

现在我正在手动编码,这不是我认为的最好的解决方案。但是,我不能只存储数组中的类,因为类类型不同,如果我使用 object[],我不能调用 runRandomExercise() 方法。有什么聪明的方法来处理这个吗?

到目前为止,这是我的代码。它有效,但添加其他类会很痛苦......

/*Specific classes that extend abstract class TrainingClass with the runRandomExercise() method*/
private MatheMagic mMathMag;
private Mnemonics mMnemonics;
private String[] mTrainingClasses;


/*Initialize classes*/
mMathMag = new MatheMagic();
mMnemonics = new Mnemonics();

/*Manually store classe names*/
mTrainingClasses = new String[2];
mTrainingClasses[0] = "mMathMag";
mTrainingClasses[1] = "mMnemonics"; 


/*Return random exercise*/
public String[] RandomExercise() {
    Random aGenerator = new Random();

    /*Get random class name*/
    int rnd = aGenerator.nextInt(mTrainingClasses.length);
    String aChosen = mTrainingClasses[rnd];

    String[] aRes = new String[2];


    if (aChosen == "mMathMag") {
        aRes = mMathMag.runRandomExercise();
    } else if (aChosen == "mMnemonics") {
        aRes = mMnemonics.runRandomExercise();
    }

    return aRes;
}

编辑这是 TrainingClass 的定义方式:

/** Common interface for all exercises */ 
public interface Exercise { 
   public String[] run();
}

/** Common interface for all training classes */
 public abstract class TrainingClass {

  private Random mRandGen = new Random();
  public ArrayList<Exercise> mExerciseTypes = new ArrayList<Exercise>();

  /** Run a random exercise */
 public String[] runRandomExercise() {
    int i = mRandGen.nextInt(mExerciseTypes.size());
    return mExerciseTypes.get(i).run();
 }
}



/*Specific training class*/

public class MatheMagic extends TrainingClass {

 public MatheMagic() {

    class SomeExercise implements Exercise {

        public String[] run() {

             String[] mRes = new String[2];
              mRes[0] = "Question type 1";
              mRes[1] = "Answer type 1";
            return mRes;
        }

    }

    class SomeOtherExercise implements Exercise {

        public String[] run() {

             String[] mRes = new String[2];
              mRes[0] = "Question type 2";
              mRes[1] = "Answer type 2";
            return mRes;
        }

    }
    SomeExercise mN = new SomeExercise();

    SomeOtherExercise mS = new SomeOtherExercise();

    mExerciseTypes.add(mN);
    mExerciseTypes.add(mS);
 } 

}
4

2 回答 2

4

简单的解决方案是使用通用方法创建一个接口,并让所有类扩展它。

创建该类型的集合或数组而不是 Object;您可以简单地迭代或随机选择并调用您想要的方法。

对我来说,这感觉就像是 GoF 的命令模式。

public interface Exercise {
    void execute();
}

现在你的课程这样做:

public class MatheMagic implements Execise {
    public void execute() {
        // special logic here.
    }
}

然后你可以这样做:

int numExercises = 1;
Exercise [] exercises = new Exercise[numExercises];
exercises[0] = new MatheMagic();
for (Exercise exercise : exercises) {
    exercise.execute();
}
于 2012-09-03T23:33:57.303 回答
1

是的,是的,您可以将所有这些类存储在一个数组中,然后随机调用它们。如何?创建一个接口,并在所有类中派生自该接口。这样您就可以基于接口调用,而不是基于实现调用。

于 2012-09-03T23:34:38.477 回答