は、私は、彼らがすべて実装することでこの一般的なインターフェイスを作成することは可能ですか?
public static class M2SA
{
// Median of 2 sorted arrays
public static int Method (int[] A, int[] B)
{
int m = A.Length, n = B.Length;
if((m | n) == 0)
throw new ArgumentException("A and B cannot both be empty");
int i = 0, j = 0, k = (m + n)/2;
while((i + j) < k)
{
if(i == m) ++j;
else if(j == n || A[i] <= B[j]) ++i;
else ++j;
}
if(i == m) return B[j];
else if(j == n) return A[i];
else return Math.Min(A[i],B[j]);
}
public static int Alternative (int[] A, int[] B)
{
if ((A.Length | B.Length) == 0)
throw new ArgumentException("A and B cannot both be empty");
int[] mergedAndSorted = A.Concat(B).OrderBy(x => x).ToArray();
return mergedAndSorted[mergedAndSorted.Length/2];
}
public static bool Test()
{
return false;//Placeholder - haven't implemented yet
}
}
のようなクラスの多くを作成するつもりです
- という名前
public static
方法Method
- 同じを持っている
Alternative
という名前public static
方法署名がMethod
- のように、
public static bool Test
として、Method
およびAlternative
は、生成された入力のセットに対して同等の出力を生成します。
これらのクラスには、ヘルパーとして機能する他のメソッドが含まれている場合があります。
一般的なインターフェイスを一般的に作成する方法はありますか?上記以外には何も分かりません。あるいは、そのメソッドに特定のシグネチャが必要なのでしょうか?
例えば、私は、だから私は
public static interface InterviewQuestion
{
public static Method;
public static Alternative;
public static bool Test();
}
(私は以下の知っている完全無効です...)のようなものであるインターフェイスをしたい
public static class UnstablePartition
{
public static void intswap(ref int a, ref int b)
{
// I'm amazed that there isn't already a method for this in the .NET library (???)
int temp = a;
a = b;
b = temp;
}
public delegate bool UnaryPredicate (int i);
public static void Method (int[] arr, UnaryPredicate pred)
{
for(int i = 0, j = arr.Length; i < j;)
{
if (!pred(arr[i])) ++i;
else if (pred(arr[j])) --j;
else intswap(ref arr[i],ref arr[j]);
}
}
public static void Alternative(int[] arr, UnaryPredicate pred)
{
int[] partioned = new int[arr.Length];
for (int ai = 0, pi = 0, pj = partioned.Length; ai < arr.Length; ++ai)
{
if (pred(arr[ai])) partioned[pj--] = arr[ai];
else partioned[pi++] = arr[ai];
}
Array.Copy(partioned, arr, partioned.Length);
}
public static bool Test()
{
return false;//Placeholder - haven't implemented yet
}
}
のような別のクラスを持っているかもしれませんそれから私はそれを実装します
public static class M2SA : InterviewQuestion
スタティックメンバーを持つスタティックインターフェイスを作成することはできますか。 –
インターフェースは 'static'でもメンバーでもありません – MickyD
" static interface "のポイントは何でしょうか?静的継承はありません。静的メソッドを呼び出す場合は、正確にどのクラスを指定しているのでしょうか。この問題を解決する方法は、クラスを非静的にすることです。 – Blorgbeard