[英]Whats the reasoning behind not allowing a derived class's concrete property that implements an interface's interface property?
[英]Is there any way to make each class that implements interface Ae has a List of one of the concrete classes that implement Ba as property?
有没有办法让每个实现接口 Ae 的 class 都有一个将 Ba 实现为属性的具体类的列表?
public interface IQuestion
{
IAnswerOption[] Answers { get; set; }
}
public interface IAnswerOption
{
int Id { get; }
bool IsCorrect { get; set; }
}
public class AnswerOptionText : IAnswerOption
{
public int Id { get; }
public bool isCorrect;
public string ansText;
}
public class AnswerOptionImage : IAnswerOption
{
public int Id { get; }
public bool isCorrect;
public string imgSlug;
}
public class AudioQuestion : IQuestion
{
public AnswerOptionImage[] Answers;
public string audioName;
}
public class TextQuestion : IQuestion
{
public AnswerOptionText[] Answers { get; set; }
public string questionText { get; set; }
}
当我尝试时, AudioQuestion和TextQuestion不允许我分别使用AnswerOptionImage[]和AnswerOptionText[] 。
Visual Studio 说我需要实现接口成员 IQuestion.Answers,但这不是我想要的。
如果有人可以帮助我,我将不胜感激。 谢谢。
似乎为您的IQuestion
接口使用generics将是一个不错的选择:
public interface IQuestion<T> where T: IAnswerOption
{
T[] Answers { get; set; }
}
public class AudioQuestion : IQuestion<AnswerOptionImage>
{
public AnswerOptionImage[] Answers{ get; set; }
public string audioName;
}
public class TextQuestion : IQuestion<AnswerOptionText>
{
public AnswerOptionText[] Answers { get; set; }
public string questionText { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.