我正在尝试为某些评估软件开发API,这将允许我创建包含问题文本,多项选择文本和正确答案的选择索引的问题。
所有问题都值10分,问题只能回答一次。
对于QuestionTypeA,用户可以更改重试次数和QuestionTypeB,用户可以更改重试次数和分数。
CommonFunctions接口的原因是我不必在每个QuestionType接口中复制QuestionText,AddChoice和SetAnswer。
但是这会留下空接口,我不确定是否正确?
另外,要实例化一个我必须写的对象
QuestionTypeA QTA = (QuestionTypeA)new Question();
我希望Question类处理了强制转换,这样用户就可以输入:
QuestionTypeA QTA = new Question();
如何实现这一目标?
由于 安德鲁
public interface IRetryable
{
int Retries { set; get; }
}
public interface IScoreable
{
int Score { set; get; }
}
public interface CommmonFunctions
{
string QuestionText { set; get; }
void AddChoice(string ChoiceText);
int SetAnswer { set; get; }
}
public interface QuestionTypeA : CommmonFunctions, IRetryable
{
}
public interface QuestionTypeB : CommmonFunctions, IRetryable, IScoreable
{
}
public interface QuestionTypeC : CommmonFunctions
{
}
class Question : CommmonFunctions
{
private List<string> Choices;
public Question()
{
Choices = new List<string>();
Retries = 1;
Score = 10;
}
public string QuestionText { set; get; }
public void AddChoice(string ChoiceText)
{
Choices.Add(ChoiceText);
}
public int SetAnswer { set; get; }
public int Retries { set; get; }
public int Score { set; get; }
}
答案 0 :(得分:1)
看来你过于思考......
保持简单。
var question1 = new QuestionTypeA(); var question2 = new QuestionTypeB();
public interface IRetryable
{
int Retries { set; get; }
}
public interface IScoreable
{
int Score { set; get; }
}
public abstract class Question
{
private List<string> choices = new List<string>();
public string QuestionText { set; get; }
public int SetAnswer { set; get; }
public void AddChoice(string choiceText)
{
choices.Add(choiceText);
}
}
public class QuestionTypeA : Question, IRetryable
{
public QuestionTypeA()
{
Retries = 1;
}
public int Retries { set; get; }
}
public class QuestionTypeB : Question, IScoreable
{
public QuestionTypeB()
{
Score = 0;
}
public int Score { set; get; }
}
答案 1 :(得分:0)
由于所有问题至少得分为10分和0分或更多,所以为什么不在这样的公共界面中包含该信息呢?
public interface CommmonFunctions
{
string QuestionText { set; get; }
void AddChoice(string ChoiceText);
int SetAnswer { set; get; }
int Score { set; get; }
bool ScoreReadonly {get;}
int Retries { set; get; }
bool RetriesReadonly {get;}
}
ScoreReadonly
和RetriesReadonly
显示分数或重试次数是否可以在课外更改:
class Question : CommmonFunctions
{
private List<string> Choices;
public Question()
{
Choices = new List<string>();
_retries = 1;
_score = 10;
}
public string QuestionText { set; get; }
public void AddChoice(string ChoiceText)
{
Choices.Add(ChoiceText);
}
public int SetAnswer { set; get; }
private int _retries;
public int Retries
{
get{ return _retries; }
set
{
if (!RetriesReadonly)
_retries = value;
}
}
private int _score;
public int Score
{
get{ return _score; }
set
{
if (!ScoreReadonly)
_score = value;
}
}
public virtual bool ScoreReadonly {get{return false;}}
public virtual bool RetriesReadonly {get{return false;}}
}
更新
public interface IShuffable
{
void Shuffle();
}
class QuestionTypeA : Question, IShuffable
{
// question of type A can't change score
public override bool ScoreReadonly { get {return true;}}
public void Shuffle()
{
// Code to implement shuffling the choices
}
}
class QuestionTypeB : Question {}
答案 2 :(得分:0)
感谢ASh的回复。 我想将它们合并到界面中是有意义的。
我现在需要在QuestionTypeA中添加另一个接口(IShuffle),如下所示。
interface IShuffable
{
void Shuffle();
}
public class QuestionTypeA : Question, IRetryable, IShuffable
{
public QuestionTypeA()
{
Retries = 1;
}
public int Retries { set; get; }
public void Shuffle()
{
// Code to implement shuffling the choices
}
}
Shuffle与QuestionType B或C无关,所以理想情况下我想将此方法隐藏在QuestionType B和C的实例中,以便它不会出现在intellisense中。
任何尝试在QuestionTypeB或C的实例上调用shuffle方法都应该导致标准的编译器错误,即&#34; QuestionTypeB不包含shuffle的定义...&#34;。
鉴于上述情况,为此解决方案使用接口会更好吗?或其他什么?
谢谢Andrew