如何在C#中正确地将不同接口的混合添加到不同的类中
本文关键字:混合 添加 接口 正确地 | 更新日期: 2024-10-24 03:03:49
我正在尝试为某些评估软件开发API,该软件将允许我创建由问题文本、多选文本和正确答案的选择索引组成的问题。
所有问题都得10分,而且这个问题只能回答一次。
然而,对于QuestionTypeA,用户可以更改重试次数,而QuestionType B,用户可以同时更改重试次数和分数。
CommonFunctions接口的原因是,我不必在每个QuestionType接口中重复QuestionText、AddChoice和SetAnswer。
但这确实给我留下了空接口,我不确定这是否正确?
此外,要实例化一个对象,我必须编写
QuestionTypeA QTA = (QuestionTypeA)new Question();
我宁愿Question类处理演员阵容,这样用户就可以键入:
QuestionTypeA QTA = new Question();
如何做到这一点?
谢谢Andrew
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; }
}
你似乎想得太多了。。。
保持简单。
var question1=新的QuestionTypeA();var question2=新的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; }
}
感谢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无关,所以理想情况下,我想对QuestionTypeB和C的实例隐藏此方法,这样它就不会出现在intelligense中。
在QuestionTypeB或C的实例上调用shuffle方法的任何尝试都会导致标准编译器错误,即"QuestionType B不包含shuffle的定义…"。
考虑到以上情况,使用接口解决方案会更好吗?还是别的什么?
感谢Andrew
既然所有问题都有至少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 {}