C#3.0では、「this」という命名法を使用してメソッドを拡張できることを私は知っています。
新しいクラスを含めるためにMath.Cos(ダブルラジアン)を拡張しようとしています。既存のクラスで「Cos」メソッドを作成できることは知っていますが、演習のためにこれをどのように/実行できるかを知りたいだけです。
いくつかの新しいことを試した後、入力を得るためにSOに戻ります。私は立ち往生しています。
これが私がこの時点で持っているものです...
public class EngMath
{
/// ---------------------------------------------------------------------------
/// Extend the Math Library to include EngVar objects.
/// ---------------------------------------------------------------------------
public static EngVar Abs(this Math m, EngVar A)
{
EngVar C = A.Clone();
C.CoreValue = Math.Abs(C.CoreValue);
return C;
}
public static EngVar Cos(this Math m, EngVar A)
{
EngVar C = A.Clone();
double Conversion = 1;
// just modify the value. Don't modify the exponents at all
// is A degrees? If so, convert to radians.
if (A.isDegrees) Conversion = 180 / Math.PI;
C.CoreValue = Math.Cos(A.CoreValue * Conversion);
// if A is degrees, convert BACK to degrees.
C.CoreValue *= Conversion;
return C;
}
...