如何正确地将不同接口的混合添加到C#中的不同类中?
我正在尝试为一些评估软件开发一个API,它允许我创建由问题文本、多项选择文本和正确答案的选择索引组成的问题 所有问题均得10分,问题只能回答一次 然而,对于QuestionTypeA,用户可以更改重试次数,而QuestionTypeB,用户可以更改重试次数和分数 CommonFunctions接口的原因是,我不必在每个QuestionType接口中重复QuestionText、AddChoice和SetAnswer 但这确实给我留下了空接口,我不确定这是否正确 此外,要实例化一个对象,我必须编写如何正确地将不同接口的混合添加到C#中的不同类中?,c#,interface,C#,Interface,我正在尝试为一些评估软件开发一个API,它允许我创建由问题文本、多项选择文本和正确答案的选择索引组成的问题 所有问题均得10分,问题只能回答一次 然而,对于QuestionTypeA,用户可以更改重试次数,而QuestionTypeB,用户可以更改重试次数和分数 CommonFunctions接口的原因是,我不必在每个QuestionType接口中重复QuestionText、AddChoice和SetAnswer 但这确实给我留下了空接口,我不确定这是否正确 此外,要实例化一个对象,我必须编写
QuestionTypeA QTA = (QuestionTypeA)new Question();
我希望问题类处理cast,这样用户就可以键入:
QuestionTypeA QTA = new Question();
如何才能做到这一点
谢谢
安德鲁
公共接口IRetryable
{
int重试{set;get;}
}
公共接口是可收费的
{
整数分数{set;get;}
}
公共接口函数
{
字符串QuestionText{set;get;}
void AddChoice(字符串选项text);
int SetAnswer{set;get;}
}
公共接口问题类型A:CommmonFunctions,IRetryable
{
}
公共接口问题类型B:CommmonFunctions、IRetryable、iScore
{
}
公共接口问题类型C:CommmonFunctions
{
}
课堂提问:commmon函数
{
私人名单选择;
公众问题(
{
选项=新列表();
重试次数=1次;
得分=10分;
}
公共字符串QuestionText{set;get;}
public void AddChoice(字符串选项text)
{
选项。添加(ChoiceText);
}
公共int SetAnswer{set;get;}
公共int重试{set;get;}
公共整数分数{set;get;}
}
看来你想得太多了
保持简单
var question1=新的QuestionTypeA();
var question2=新的QuestionTypeB()
公共接口IRetryable
{
int重试{set;get;}
}
公共接口是可收费的
{
整数分数{set;get;}
}
公共抽象类问题
{
私有列表选项=新列表();
公共字符串QuestionText{set;get;}
公共int SetAnswer{set;get;}
public void AddChoice(字符串选项text)
{
选项。添加(choiceText);
}
}
公开课问题类型A:问题,IRetryable
{
公共问题A()
{
重试次数=1次;
}
公共int重试{set;get;}
}
公开课问题类型B:问题,可受理
{
公共问题类型b()
{
得分=0;
}
公共整数分数{set;get;}
}
既然所有的问题都至少有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
和重试次数重复
显示是否可以在课外更改分数或重试次数:
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;}}
}
谢谢你的回复。 我想将它们合并到接口中是有意义的 我现在需要向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方法的任何尝试都会导致标准编译器错误,即“QuestionTypeB不包含shuffle的定义…”
鉴于上述情况,使用此解决方案的接口是否更好?还是别的什么
谢谢安德鲁我建议你读一下关于装饰图案的书
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 {}
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
}
}