[英]How to encapsulate a property in a base class?
我的情况是关于开发数学问题。 作为IProblem
接口,我认为它应该包含的两个主要属性是QuestionText
和Response
。 QuestionText
始终是一个字符串,但是Response
有时可以是一个复杂的对象(自定义Fraction
结构)或其他数据类型,例如string,decimal,int等。
public interface IProblem
{
string QuestionText { get; set; }
object Response { get; }
bool IsComplete();
bool IsCorrect();
}
如您所见, Response
是对象。 我猜这个数据类型是因为本质上所有问题都有响应。 作为一个对象,我仅定义针对将来的错误(铸造问题)的get。
我的想法是,稍后,在一个具体类中访问此属性( Response
),而无需强制转换。 看看这个?
public abstract class Problem : IProblem
{
public string QuestionText { get; set;}
public object Response { get; protected set; }
public virtual bool IsComplete()
{
return true;
}
public abstract bool IsCorrect();
}
public class BinaryProblem : Problem
{
public decimal N1 { get; set; }
public decimal N2 { get; set; }
public decimal Response
{
get { return (decimal)base.Response; }
set { base.Response = value; }
}
public override bool IsCorrect()
{
return N1 + N2 == Response;
}
}
在这里,我正在测试其价值。
static void Main(string[] args)
{
BinaryProblem p = new BinaryProblem();
p.N1 = 2;
p.N2 = 4;
p.Response = 6;
IProblem p2 = p;
Console.WriteLine(p2.Response);
Console.WriteLine(p2.IsComplete().ToString());
}
到目前为止,它仍然有效,但是我想知道我在做什么是正确的还是好的做法。 我已经看到其他人使用new
运算符来执行此操作。 其他人则不使用base
这个词。
这是个好方法吗? 会引起将来的错误吗? 请给我有关我的设计的反馈。
编辑:确实有必要在非通用接口中访问响应。
也许您正在寻找这样的东西? 注意,我遗漏了一些东西,因为它们对于问题的通用解决方案部分并不重要(例如QuestionText)。 我还省略了基类,因为它似乎不过是一个传递和一个多余的不必要的层。 这可能并非您要找的东西,但我希望它能帮助您到达那里。
首先,这是所有内容的用法:
编辑:注意现在如何将它们全部视为非通用IProblem。
private static void StackOverflowQuestion()
{
IProblem<int> problem1 = new IntProblem(2, 4);
problem1.Response = 6;
IProblem<decimal> problem2 = new DecimalProblem(5, 10);
problem2.Response = .5M;
Console.WriteLine("Problem 1 is correct: {0}", problem1.IsCorrect());
Console.WriteLine("Problem 2 is correct: {0}", problem2.IsCorrect());
List<IProblem> problems = new List<IProblem>();
problems.Add(problem1);
problems.Add(problem2);
problems.ForEach(problem => Debug.WriteLine(problem.GetResponse()));
}
编辑:这是非通用接口,因此可以在列表中使用许多问题并以相同的方式处理:
public interface IProblem
{
object GetResponse();
}
这是界面:
编辑:请注意,这现在实现了非通用接口。
public interface IProblem<T> : IProblem
{
T Response { get; set; }
bool IsCorrect();
}
这是类:
编辑:注意新的GetResponse()方法。
public class IntProblem : IProblem<int>
{
private int _number1 { get; set; }
private int _number2 { get; set; }
public int Response { get; set; }
public IntProblem(int number1, int number2)
{
this._number1 = number1;
this._number2 = number2;
}
public bool IsCorrect()
{
return this._number1 + this._number2 == Response;
}
public object GetResponse()
{
return this.Response;
}
}
public class DecimalProblem : IProblem<decimal>
{
private decimal _number1 { get; set; }
private decimal _number2 { get; set; }
public decimal Response { get; set; }
public DecimalProblem(decimal number1, decimal number2)
{
this._number1 = number1;
this._number2 = number2;
}
public bool IsCorrect()
{
return this._number1 / this._number2 == Response;
}
public object GetResponse()
{
return this.Response;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.