简体   繁体   English

公共静态方法+接口

[英]public static method + interface

Since we can't define a public static method in the interface, can such an interface be implemented in a class with public static? 由于我们无法在接口中定义公共静态方法,这样的接口是否可以在具有public static的类中实现?

public interface IValidator
{
    bool IsValid(bool data);
}

public class MyValidator : IValidator
{
    public static bool IsValid(string data)
    {
        //code which returns bool
    }
}

No, C# does not allow for static interfaces. 不,C#不允许静态接口。

Interfaces are designed to act as contracts between classes, that contract defines that each instance of these classes will have a set of method. 接口被设计为充当类之间的契约,该契约定义这些类的每个实例都将具有一组方法。

Jon Skeet has given a very good explanation in this question , I'd recommend reading it. Jon Skeet在这个问题上做了很好的解释,我建议你阅读它。

When you have an object instance, it makes sense to cast and use that as an interface. 当你有一个对象实例时,将它作为一个接口进行转换和使用是有意义的。 But when you work with static stuff, this isn't the case. 但是当你使用静态的东西时,情况并非如此。 You can only access static members through the name of the containing class, you can't pass them around like instances, etc. 您只能通过包含类的名称访问静态成员,不能像实例一样传递它们等。

It is possible to implement an interface and make sure it's not instantiated multiple times, it's called the singleton pattern. 可以实现一个接口并确保它没有多次实例化,它被称为单例模式。 A singleton class is similar to a static class, but it has an instance that can be passed around and it can implement interfaces as well. 单例类与静态类类似,但它有一个可以传递的实例,它也可以实现接口。

No, but you could get something close to it by having a static member return an instance of the interface. 不,但是你可以通过让静态成员返回接口的实例来获得接近它的东西。
Somthing like: 像这样的东西:

public class MyValidator : IValidator
{
    public bool IsValid(string data)
    {
        //code which returns bool
    }

    public static readonly IValidator Instance = new MyValidator();
}

Then you could use it in a static sort of way: 然后你可以以静态的方式使用它:

bool isValid = MyValidator.Instance.IsValid("data");

You cannot define static members via the interface, so if static members are required by design they can only be added to concrete types. 您无法通过接口定义静态成员,因此如果设计需要静态成员,则只能将其添加到具体类型。

This ultimately produces a lot of confusion. 这最终会产生很多混乱。 Any other implementation would not have that same member. 任何其他实现都不会有相同的成员。 Mocked instances will not have access to that member. 模拟实例将无法访问该成员。 And so on. 等等。

Solution is to avoid declaring static members. 解决方案是避免声明静态成员。 In your particular case, I would object to the very design of the interface. 在您的特定情况下,我会反对界面的设计。 I would prefer to see classes implement some interface like IValidatable: 我更希望看到类实现一些像IValidatable这样的接口:

public interface IValidatable
{
    bool IsValid();
}
...
public class SomeBoolClass: IValidatable
{
    private bool Data;
    public bool IsValid()
    {
        return this.Data; // i.e. return Data == true
    }
}
...
public class SomeStringClass: IValidatable
{
    private string Data;
    public bool IsValid()
    {
        return !string.IsNullOrEmpty(this.Data);
    }
}

In this way you obtain fully polymorphic validation across all current and future types. 通过这种方式,您可以获得所有当前和未来类型的完全多态验证。

If you insist on having a validator which receives low-level data (such as Boolean or string) to validate, then you are half-way doomed. 如果你坚持要一个接收低级数据(如布尔或字符串)的验证器进行验证,那么你就注定要失败了。 Suppose that there are two classes which wrap string data. 假设有两个类包装字符串数据。 These two classes would normally have to be validated in different ways. 通常必须以不同方式验证这两个类。 But validator would not be able to distinguish which validation algorithm to apply based on input data. 但验证器无法根据输入数据区分应用哪种验证算法。 Even worse, validator would have to contain all validation logic for all types, existing and those yet to be coded. 更糟糕的是,验证器必须包含所有类型的所有验证逻辑,现有的和尚未编码的类型。 This means that validator would act as one giant (and inherently incomplete) switch statement. 这意味着验证器将充当一个巨大的(并且固有地不完整)switch语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM