如何使变量可以是三个预定义对象之一

时间:2016-12-01 17:13:58

标签: c# class polymorphism

我的代码允许用户创建问答对。答案有三种类型:NumericalAnswer,RangedAnswer或ShortAnswer(字符串)。

我有基础问题类:

class QuestionClass
{
    private string question;
    private AnswerClass answer;

    public string Question
    {
        get { return question; }
        set { question = value; }
    }

    public AnswerClass Answer
    {
        get { return answer; }
        set { answer = value; }
    }

    public QuestionClass(string question, AnswerClass answer)
    {
        this.question = question;
        this.answer = answer;
    }
}

所有答案类型都是从一个空的基类AnswerClass继承而来的。答案类是:

class NumericalAnswer : AnswerClass
{
    private float answer;

    public float Answer
    {
        get { return answer; }
        set { answer = value; }
    }
}

class RangedAnswer : AnswerClass
{
    private float lowerBound;
    private float upperBound;

    public float LowerBound
    {
        get { return lowerBound; }
        set { lowerBound = value; }
    }

    public float UpperBound
    {
        get { return upperBound; }
        set { upperBound = value; }
    }
}

class ShortAnswer : AnswerClass
{
    private string[] answer;

    public string[] Answer
    {
        get { return answer; }
        set { answer = value; }
    }
}

但是,例如,如果我使用RangedAnswer创建一个问题并尝试访问lowerBound变量,我就不能这样做。如果您知道我将如何拥有多个答案类。

1 个答案:

答案 0 :(得分:0)

您可以使用泛型:

public class QuestionClass<T> where T : AnswerClass
{
    private string question;
    private T answer;

    public string Question
    {
        get { return question; }
        set { question = value; }
    }

    public T Answer
    {
        get { return answer; }
        set { answer = value; }
    }

    public QuestionClass(string question, T answer)
    {
        this.question = question;
        this.answer = answer;
    }
}

样本用法:

var ra = new RangedAnswer();
ra.LowerBound = 10;
ra.UpperBound = 20;
var q = new QuestionClass<RangedAnswer>("Test range?", ra);
q.Answer.LowerBound.Dump();

编辑支持创建包含多种问题类型的列表..

这样的事情会起作用吗?

    public abstract class QuestionClass { 

    }

    public class QuestionClass<T> : QuestionClass where T: AnswerClass
    {
        private string question;
        private T answer;

        public string Question
        {
            get { return question; }
            set { question = value; }
        }

        public T Answer
        {
            get { return answer; }
            set { answer = value; }
        }

        public QuestionClass(string question, T answer)
        {
            this.question = question;
            this.answer = answer;
        }
    }

And usage like this:

void Main()
{
    var ra = new RangedAnswer();
    ra.LowerBound = 10;
    ra.UpperBound = 20;

    var na = new NumericalAnswer();
    na.Answer = 25;

    var q1 = new QuestionClass<RangedAnswer>("Test range?", ra);
    var q2 = new QuestionClass<NumericalAnswer>("Numberic?", na);

    var list = new List<QuestionClass>();
    list.Add(q1);
    list.Add(q2);

    foreach (var q in list)
    {
        var rat = q as QuestionClass<RangedAnswer>;
        if (rat != null) {
            rat.Answer.LowerBound.Dump();
        }
        var nat = q as QuestionClass<NumericalAnswer>;
        if (nat != null) {
            nat.Answer.Answer.Dump();           
        }
    }
}