C# 是否可以创建一个这样通用的接口?
我将创建很多类,比如C# 是否可以创建一个这样通用的接口?,c#,.net,oop,inheritance,C#,.net,Oop,Inheritance,我将创建很多类,比如 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
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 class M2SA : InterviewQuestion
可以包含方法、属性、事件、索引器或这四种成员类型的任意组合。。。接口不能包含常量、字段、运算符、实例构造函数、析构函数或类型。接口成员自动公开,并且不能包含任何访问修饰符成员也不能是静态的。
这意味着您的界面看起来像
public interface InterviewQuestion
{
int Method(int[] a, int[] b);
int Alternative(int[] a, int[] b);
bool Test();
}
public class M2SA : InterviewQuestion
{
public int Alternative(int[] a, int[] b)
{
// implementation
}
public int Method(int[] a, int[] b)
{
// implementation
}
public bool Test()
{
// implementation
}
}
public class UnstablePartition : InterviewQuestion
{
public int Alternative(int[] a, int[] b)
{
// implementation
}
public int Method(int[] a, int[] b)
{
// implementation
}
public bool Test()
{
// implementation
}
}
public abstract class InterviewQuestion
{
public abstract int Method(int[] a, int[] b);
public abstract int Alternative(int[] a, int[] b);
public abstract bool Test();
}
public class M2SA : InterviewQuestion
{
public override int Alternative(int[] a, int[] b)
{
// implementation
}
public override int Method(int[] a, int[] b)
{
// implementation
}
public override bool Test()
{
// implementation
}
}
public class UnstablePartition : InterviewQuestion
{
public override int Alternative(int[] a, int[] b)
{
// implementation
}
public override int Method(int[] a, int[] b)
{
// implementation
}
public override bool Test()
{
// implementation
}
}
你的课看起来像
public interface InterviewQuestion
{
int Method(int[] a, int[] b);
int Alternative(int[] a, int[] b);
bool Test();
}
public class M2SA : InterviewQuestion
{
public int Alternative(int[] a, int[] b)
{
// implementation
}
public int Method(int[] a, int[] b)
{
// implementation
}
public bool Test()
{
// implementation
}
}
public class UnstablePartition : InterviewQuestion
{
public int Alternative(int[] a, int[] b)
{
// implementation
}
public int Method(int[] a, int[] b)
{
// implementation
}
public bool Test()
{
// implementation
}
}
public abstract class InterviewQuestion
{
public abstract int Method(int[] a, int[] b);
public abstract int Alternative(int[] a, int[] b);
public abstract bool Test();
}
public class M2SA : InterviewQuestion
{
public override int Alternative(int[] a, int[] b)
{
// implementation
}
public override int Method(int[] a, int[] b)
{
// implementation
}
public override bool Test()
{
// implementation
}
}
public class UnstablePartition : InterviewQuestion
{
public override int Alternative(int[] a, int[] b)
{
// implementation
}
public override int Method(int[] a, int[] b)
{
// implementation
}
public override bool Test()
{
// implementation
}
}
或者你可以使用。那么您的实现看起来像
public interface InterviewQuestion
{
int Method(int[] a, int[] b);
int Alternative(int[] a, int[] b);
bool Test();
}
public class M2SA : InterviewQuestion
{
public int Alternative(int[] a, int[] b)
{
// implementation
}
public int Method(int[] a, int[] b)
{
// implementation
}
public bool Test()
{
// implementation
}
}
public class UnstablePartition : InterviewQuestion
{
public int Alternative(int[] a, int[] b)
{
// implementation
}
public int Method(int[] a, int[] b)
{
// implementation
}
public bool Test()
{
// implementation
}
}
public abstract class InterviewQuestion
{
public abstract int Method(int[] a, int[] b);
public abstract int Alternative(int[] a, int[] b);
public abstract bool Test();
}
public class M2SA : InterviewQuestion
{
public override int Alternative(int[] a, int[] b)
{
// implementation
}
public override int Method(int[] a, int[] b)
{
// implementation
}
public override bool Test()
{
// implementation
}
}
public class UnstablePartition : InterviewQuestion
{
public override int Alternative(int[] a, int[] b)
{
// implementation
}
public override int Method(int[] a, int[] b)
{
// implementation
}
public override bool Test()
{
// implementation
}
}
你可以用这个模式来解决这个问题。引自该网站:
单一状态是一个“概念上的单态”——一个单一状态的所有数据成员
monostate是静态的,因此monostate的所有实例都使用相同的
(静态)数据。使用单状态的应用程序可以创建任意数量的
实例,因为每个实例使用相同的数据。什么
我发现单状态类非常好的地方在于它们避免了所有的
必须访问类的特定实例的复杂性。
任何例子都一样好。我发现单态确实增加了
对于那些沉浸在GOF单身人群中的人来说,这可能是一种困惑
一件坏事
基本上,不是使用静态类,而是将它们设置为常规类。然而,在内部,它们的行为是静态的。成员变量是静态的,因此它们可以在类的实例之间共享
这种方法允许您针对类实现接口。这还意味着,如果需要,您可以通过这些接口注入依赖项类。此外,如果您决定要/需要特定于实例的数据,则更容易实现
注意:在您的情况下,没有状态,因此您应该实现该模式,以允许您针对具体类型实现接口或抽象类。其他答案评论了您试图创建静态接口的事实 我认为这里更深层次的问题是,您试图创建一个具有不一致方法签名的接口 是否有一种方法可以创建一个足够通用的接口,它需要上述内容,但除此之外我什么都不知道?或者它会要求其方法具有某些特征吗 您基本上要求创建一个带有未知方法参数的部分接口。接口必须具有定义良好的签名 您可以将参数分离到不同的接口或类,例如
public interface IInterviewQuestion
{
int Method(IQuestionInput qParams);
int Alternative(IQuestionInput qParams);
bool Test();
}
即使如此,方法的返回类型必须是常量不变的
当然,
IQuestionInput
可能包含任何内容,它的用处取决于您需要如何使用这些问题。类似的东西怎么样
public interface InterviewQuestion<T, TResult>
{
TResult Method (Func<T, TResult> func);
TResult Alternative (Func<T, TResult> func);
bool Test ( );
}
public class M2SA : InterviewQuestion<Tuple<int[], int[]>, int>
{
private static Lazy<M2SA> lazy = new Lazy<M2SA>(() => new M2SA(), true);
public static M2SA Instance => lazy.Value;
public int Alternative (Func<Tuple<int[], int[]>, int> func)
{
throw new NotImplementedException();
}
public int Method (Func<Tuple<int[], int[]>, int> func)
{
throw new NotImplementedException();
}
public bool Test ( )
{
throw new NotImplementedException();
}
}
公共界面访谈问题
{
TResult方法(Func-Func);
TResult替代方案(Func Func);
布尔检验();
}
公共课M2SA:访谈问题
{
private static Lazy Lazy=new Lazy(()=>new M2SA(),true);
公共静态M2SA实例=>lazy.Value;
公共整数可选(Func Func)
{
抛出新的NotImplementedException();
}
公共int方法(Func Func)
{
抛出新的NotImplementedException();
}
公共布尔测试()
{
抛出新的NotImplementedException();
}
}
您仍然使用常规接口并使用
单例模式来模拟静态类您不能有静态接口。要使接口受益,必须使其成为常规成员方法
您可以使用以下界面:
public interface InterviewQuestion<T1, T2>
{
public Method<T1 arg1, T2 arg2>();
public Alternative<T1 arg1, T2 arg2>();
public bool Test();
}
访问:
InterviewQuestions.M2SA.Method(blah, blah);
在您开始编写没有静态绑定到其中一个实现的代码之前,这没有多大价值。您可以通过搜索条件(如T1或T2或两者)或其他方式找到实现。您可以创建一个没有泛型参数的接口,并为方法提供对象参数,这样就可以在不知道类型的情况下调用它。等等。是否可以创建带有静态成员的静态接口?。。。不可以。接口不能是静态的,也不能是它的成员“静态接口”的要点是什么?这里没有静态继承——如果调用静态方法,则指定的是确切的类。解决此问题的方法是使类成为非静态的。即使可以在接口中定义静态方法。。。如果他们有不同的签名,那又有什么意义呢?编译器如何知道这些静态方法需要哪些参数?这与问题直接相关吗?OP的类是无状态的——实际上看起来它们不需要是静态类。@TimBarrass——它们是无状态的并不重要。它们实际上是单例的,他希望实现针对它们的接口。单状态模式允许这种情况,只是在他的情况下,没有状态!根据你的描述,一个没有任何状态的单状态只是一个类——我遗漏了什么吗?@timbarass——区别在于它不是静态的。现在没有状态,我猜这就是为什么他把它们变成静态的。然而,他遇到了一个语言障碍,那就是他不能在上面实现接口。这种方法绕过了这一点,即使实例没有状态,所以在概念上是静态的。如果他确实需要添加状态,他也会被覆盖。如果它们是静态的,因为它们没有状态,那么可以合理地假设拥有状态不是一个要求。使用单状态(虽然很有趣,但我以前没有将其视为命名模式)在很大程度上是不相关的,因为有用的只是将它们转换为普通类。我同意。接口不能是静态的。所以你只需要