C# 如何在接口中将方法设为私有?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1383434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:53:56  来源:igfitidea点击:

How can I make a method private in an interface?

c#interface

提问by SmartStart

I have this interface:

我有这个界面:

public interface IValidationCRUD
{
    public ICRUDValidation IsValid(object obj);
    private void AddError(ICRUDError error);
}

But when I use it (Implement Interface, automatic generation of code), I get this:

但是当我使用它(实现接口,代码的自动生成)时,我得到了这个:

public class LanguageVAL : IValidationCRUD
{   
    public ICRUDValidation IsValid(object obj)
    {
        throw new System.NotImplementedException();
    }

    public void AddError(ICRUDError error)
    {
        throw new System.NotImplementedException();
    }   
}

The method AddError is public and not private as I wanted.

AddError 方法是公共的,而不是我想要的私有方法。

How can I change this?

我怎样才能改变这个?

采纳答案by Botz3000

An interface can only have public methods. You might consider using an abstract base class with a protected abstract method AddErrorfor this. The base class can then implement the IValidationCRUDinterface, but only after you have removed the private method.

一个接口只能有公共方法。AddError为此,您可能会考虑使用带有受保护抽象方法的抽象基类。然后基类可以实现IValidationCRUD接口,但必须在您删除私有方法之后。

like this:

像这样:

public interface IValidationCRUD
{
    ICRUDValidation IsValid(object obj);
}

public abstract class ValidationCRUDBase: IValidationCRUD {
    public abstract ICRUDValidation IsValid(object obj);
    protected abstract void AddError(ICRUDError error);
}

回答by Kim Gr?sman

A private member makes no sense as part of an interface. An interface is there to define a set of methods, a role, an object must always implement. Private methods, on the other hand, are implementation details, not intended for public consumption.

私有成员作为接口的一部分毫无意义。接口用于定义一组方法、一个角色、一个对象必须始终实现。另一方面,私有方法是实现细节,不供公共使用。

回答by Robban

You should probably check out the MSDN article here about interfaces: http://msdn.microsoft.com/en-us/library/ms173156.aspx

您应该在这里查看有关接口的 MSDN 文章:http: //msdn.microsoft.com/en-us/library/ms173156.aspx

回答by Robban

An interface cannot contain private fields. However, the closest behavior to "private fields" you can achieve is by using explicit implementation (explained here: http://msdn.microsoft.com/en-us/library/ms173157.aspx)

接口不能包含私有字段。但是,您可以实现的与“私有字段”最接近的行为是使用显式实现(在此处解释:http: //msdn.microsoft.com/en-us/library/ms173157.aspx

回答by anishMarokey

The rule of an interface:

接口规则:

The CLR also allows an interface to contain static methods, static fields, constants, and static constructors. However, a CLS-compliant interface must not have any of these static members because some programming languages aren't able to define or access them. In fact, C# prevents an interface from defining any static members. In addition, the CLR doesn't allow an interface to contain any instance fields or instance constructors.

CLR 还允许接口包含静态方法、静态字段、常量和静态构造函数。但是,符合 CLS 的接口不得包含任何这些静态成员,因为某些编程语言无法定义或访问它们。事实上,C# 阻止接口定义任何静态成员。此外,CLR 不允许接口包含任何实例字段或实例构造函数。

These are the rules of an interface and you can't do anything on that :)

这些是界面的规则,你不能做任何事情:)

These are not allowed

这些是不允许的

interface IIValidationCRUD
{
    static ICRUDValidation IsValid(object obj); //error
}

interface IIValidationCRUD
{
    public ICRUDValidation IsValid(object obj); //error
}

回答by LetsKickSomeAss in .net

Have you considered using :

您是否考虑过使用:

void IValidationCRUD.AddError(ICRUDError error)
    {
        throw new System.NotImplementedException();
    }

Instead of

代替

public void AddError(ICRUDError error)
{
    throw new System.NotImplementedException();
}

Sorry to join in too late.

抱歉加入太晚了。

回答by Aji

Its not a valid scenario in real case, but still you can achieve through explicit implementation of interface

在实际情况下它不是一个有效的场景,但您仍然可以通过接口的显式实现来实现

 interface Itest
 {
      void functiona();
      void functionb();
 }
class child : Itest
{
     public void functiona()
     {
     }
     void Itest.functionb()
     {
     }
}

here functionb() is explicitly implimented so if you created the object of class child you wont get functionb()

这里 functionb() 是明确实现的,所以如果你创建了 child 类的对象,你就不会得到 functionb()