这是我的抽象类问题:
public abstract class Problem<T> : IEquatable<T>{ public abstract int ResultCount { get; } protected abstract bool CheckTheAnswer(params object[] results); public abstract bool Equals(T other);}
下面是一个派生自Problem的类,Arithetic类包含数学问题中包含的所有必要内容,以及如何解决它:
public enum Operations{ Addition,Subtraction,Multiplication,division}public class Arithmetic : Problem<Arithmetic>{ public decimal Number1 { get; set; } public Operations Operation { get; set; } public decimal Number2 { get; set; } public overrIDe int ResultCount { get { return 1; } } protected overrIDe bool CheckTheAnswer(params object[] results) { if (results.Length != ResultCount) throw new ArgumentException("Only expected " + ResultCount + " arguments."); decimal result = (decimal)results[0]; switch (Operation) { case Operations.Addition: return Number1 + Number2 == result; case Operations.Subtraction: return Number1 - Number2 == result; case Operations.Multiplication: return Number1 * Number2 == result; case Operations.division: return Number1 / Number2 == result; default: throw new Exception("Operator unexpected"); } } public overrIDe bool Equals(Arithmetic other) { if (other == null) return false; return this.Number1 == other.Number1 && Number2 == other.Number2; }}public class Addition : Arithmetic{ public Addition(decimal addend1,decimal addend2) : base() { Number1 = addend1; Number2 = addend2; Operation = Operations.Addition; }}// Subtraction,Multiplication and divison here
然后我有另一个生成算术问题的类,它接收一个元组,其中包含一些指示条件的属性
interface IProblemFactory<T> where T : Problem<T>{ T Create();}public class ArithmeticProblemFactory : IProblemFactory<Arithmetic>{ private Tuple<Operations,Range,Range> _condition; public ArithmeticProblemFactory(Tuple<Operations,Range> condition) { this._condition = condition; } public Arithmetic Create() { Operations operation = _condition.Item1; decimal a = _condition.Item2.GetNumber(); decimal b = _condition.Item3.GetNumber(); switch (operation) { case Operations.Addition: return new Addition(a,b); case Operations.Subtraction: return new Subtraction(a,b); case Operations.Multiplication: return new Multiplication(a,b); case Operations.division: return new division(a,b); default: throw new Exception("Operator unexpected"); } }}
事情是……我需要有更多属性,比如Result(在算术类中只需要1,比较2个数字,我们需要两个属性结果),问题编号,时间(秒)来解决问题.
问题是,我不知道应该把这些属性放在哪里.有一种方法是在Problem类中添加其中的一些,或者创建另类这样的类:
ArithmeticProblem
>问题问题< - 这是算术类
>结果
>时间
>问题编号
我只想按要求组织我的课程.
提前致谢.
public class Arithmetic < TResult> : ...
和加法可以如下:
public class Addition : Arithmetic <decimal>...
但是如果参数的数量(如结果,时间,……)不是固定的(动态的),您可以使用字典并将它们存储在字典(它们的类型)中并编写特定的 *** 作并将它们设置为字典的值.
总结以上是内存溢出为你收集整理的c# – 怀疑如何组织我的课程全部内容,希望文章能够帮你解决c# – 怀疑如何组织我的课程所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)