我正在为问卷创建一个框架。
问卷有几个问题。我的情况是寻找一个名为 的类Question
,它支持您想要的任何答案。
我的意思是,有些问题只需要一个答案,另一些问题需要两个答案,其他问题需要字符串、整数、双精度或开发人员构建的任何新结构(例如,想象开发人员正在创建一个使用 Fraction 结构作为答案的数学问题)。
换句话说,我需要支持任何数据类型或数量的答案。
所以我正在考虑创建一个名为 的抽象类Question
,其中将包含一个Dictionary
响应。
public abstract class Question
{
protected Question(string questionText)
{
this.QuestionText = questionText;
this.Responses = new Dictionary<string, object>();
}
public string QuestionText
{
get;
set;
}
public IDictionary<string, object> Responses { get; protected set; }
}
例如,如果我创建一个新的Question
,这将是演示。
public sealed class Question1 : Question
{
public Question1(string questionText)
: base(questionText)
{
}
public int? Response1
{
get
{
int? value = null;
if (this.Responses.ContainsKey("Response1"))
value = this.Responses["Response1"] as int?;
return value;
}
set
{
this.Responses["Response1"] = value;
}
}
}
你怎么看这个想法?我的第一个疑问:我将答案包含在班级而不是另一个独立班级中是否正确。