ジェネリックスを使用して問題が発生しました。 (それが正しい場合)私はこれは私が算術問題を作成する例であるジェネリッククラス<T>のシナリオを解決するにはどうすればよいですか?
public interface IProblem<T>
{
ushort ResultCount { get; }
T[] Results { get; }
bool IsCorrect();
}
public abstract class ProblemBase<T> : IProblem<T>
{
private T[] _results;
private ushort? _resultCount;
public ushort ResultCount
{
get
{
if (_resultCount == null) throw new ArgumentNullException("_resultCount");
return (ushort)_resultCount;
}
protected set
{
if (_resultCount != value)
_resultCount = value;
}
}
public T[] Results
{
get
{
if (_results == null)
_results = new T[ResultCount];
return _results;
}
}
public abstract bool IsCorrect();
}
各問題結果(回答)を有するIProblem
と呼ばれるインターフェース、および結果を作成してい、ProblemA
と呼ばれます。配列のデータ型は
public class ProblemA: ProblemBase<decimal>
{
private decimal _number1;
private decimal _number2;
private Operators _operator;
public decimal Number1
{
get { return _number1; }
set { _number1 = value; }
}
public decimal Number2
{
get { return _number2; }
set { _number2 = value; }
}
public Operators Operator
{
get { return _operator; }
set { _operator = value; }
}
public decimal Result
{
get { return Results[0]; }
set { Results[0] = value; }
}
public ProblemA()
{
this.ResultCount = 1;
}
public override bool IsCorrect()
{
bool result;
switch (_operator)
{
case Operators.Addition:
result = this.Result == (this.Number1 + this.Number2);
break;
case Operators.Subtract:
result = this.Result == (this.Number1 - this.Number2);
break;
case Operators.Multiplication:
result = this.Result == (this.Number1 * this.Number2);
break;
case Operators.Division:
result = this.Result == (this.Number1/this.Number2);
break;
default:
throw new ArgumentException("_operator");
}
return result;
}
}
(anothers問題は多分string
、またはint
を持っているかもしれません)小数でなければなりませんのでT
がdecimal
である私は、MVVMを使用していますので、私はProblemBase<T>
などを含む各問題のためのViewModelを持っているしたいのですがプロパティ、しかしそれは一般的な、私はそれが問題のようになる場合は、IProblemViewModel
一般的に入れていると思います。
public interface IProblemViewModel : IViewModel
{
ProblemBase<T> Problem { get; set; }
}
私はObservableCollection<IProblemViewModel>
を使用するには、後に計画ので、これを言ったので、私は、私はIProblemViewModel
またはIProblemViewModel<T>
を書く場合は問題はありませんかはわかりません。 ありがとうございます。
それはそれを上書きしないために他の場所でいくつかのバグを待っNotImplementedException()を投げるの周りところで、実装を放置しないでください。 'パブリック抽象ブールIsCorrect()を実行します;'とabstactクラスが行くように、あなたは限りインターフェースを果たすが、それはコンパイルされませんオーバーライドするために失敗した任意の具体的な派生クラス。 –
@ジョンハンナは、チップジョンのおかげで、私はまだそれについて学んでいる!私は心の中で助言のための –