是否有原因无法在方法或接口中定义访问修饰符?

发布于 2024-08-09 01:14:04 字数 386 浏览 4 评论 0原文

方法可见性的责任被委托给实现该接口的类。

public interface IMyInterface
{
  bool GetMyInfo(string request);
}

在 C# 中,在方法 GetMyInfo() 生成以下错误之前设置访问修饰符 public、private 或 protected:修饰符“private”对此项无效。

是否有原因无法在方法或接口中定义访问修饰符?

(已在此处用法语提出问题)

The responsibility of the visibility of a method is relegated to the class that implements the interface.

public interface IMyInterface
{
  bool GetMyInfo(string request);
}

In C# set access modifier public, private or protected before the method GetMyInfo() generates the following error: The modifier 'private' is not valid for this item.

Is there a reason you can not define the access modifier on a method or in an interface?

(Question already asked in french here)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

给我一枪 2024-08-16 01:14:04

该接口定义了对象和调用其成员的客户端之间的契约。私有方法不能被任何其他对象访问,因此将其添加到接口中没有意义。因此,接口的所有成员都被视为公共成员。

The interface defines a contract between an object and clients that call its members. A private method cannot be accessed by any other objects so it doesn't make sense to add it to the interface. All members of an interface are considered public for this reason.

恋你朝朝暮暮 2024-08-16 01:14:04

如果您进行显式接口实现,您实际上可以在实现类中将该方法设为私有:

public interface IMyInterface
{
    bool GetMyInfo(string request);
}

public class MyClass : IMyInterface
{
    public void SomePublicMethod() { }

    bool IMyInterface.GetMyInfo(string request)
    {
        // implementation goes here
    }
}

此方法意味着 GetMyInfo 将不会成为 我的班级。只能通过将 MyClass 实例转换为 IMyInterface 来访问它:

MyClass instance = new MyClass();

// this does not compile
bool result = instance.GetMyInfo("some request"); 

// this works well on the other hand
bool result = ((IMyInterface)instance).GetMyInfo("some request");

因此,在接口的上下文中,其所有成员都将是公共的。它们可以对实现类的公共接口隐藏,但始终可以对实例进行类型转换并以这种方式访问​​成员。

You can actually make the method private in the implementing class, if you make an explicit interface implementation:

public interface IMyInterface
{
    bool GetMyInfo(string request);
}

public class MyClass : IMyInterface
{
    public void SomePublicMethod() { }

    bool IMyInterface.GetMyInfo(string request)
    {
        // implementation goes here
    }
}

This approach means that GetMyInfo will not be part of the public interface of MyClass. It can be accessed only by casting a MyClass instance to IMyInterface:

MyClass instance = new MyClass();

// this does not compile
bool result = instance.GetMyInfo("some request"); 

// this works well on the other hand
bool result = ((IMyInterface)instance).GetMyInfo("some request");

So, in the context of the interface, all its members will be public. They can be hidden from the public interface of an implementing class, but there is always the possibility to make a type cast to the instance and access the members that way.

萌梦深 2024-08-16 01:14:04

从面向对象的角度来看——封装就是数据隐藏。这意味着类内部发生的任何事情都取决于类的实现。这意味着通过合同强制执行私人成员是没有用的。

然而,使用接口的原因是因为您希望确保类遵守特定的契约并以一致的方式公开多个公共成员。

In terms of OO - encapsulation is all about data hiding. That means whatever goes on inside a class is up to the class implementation. Which means it would be useless to contractually enforce private members.

However, the reason one uses interfaces is because you want to ensure a class adheres to a specific contract and exposes several public members in a consistent way.

幽梦紫曦~ 2024-08-16 01:14:04

接口的所有方法必须具有相同的访问级别 - 以便调用者可以使用所有这些方法。但是,接口也可以是内部的(或作为嵌套接口私有)。

如果您需要不同的访问级别,请使用不同的界面。

All of the methods of an interface must have the same access level - so that the caller may use all of them. However interfaces can also be internal (or as nested interface private).

If you need different access levels use a distinct interface.

梅倚清风 2024-08-16 01:14:04

接口中的私有定义将:

  1. 不会为接口的用户提供任何好处(毕竟它是私有的)
  2. 限制实现类必须实现方法或属性
  3. 使接口的概念性质与实现细节
  4. 变得模糊 就像一个抽象具有私有方法的类(这是不允许的)

A private definition in the interface would:

  1. provide no benefit to the user of the interface (it is private after all)
  2. constrain the implementing class to have to implement the method or property
  3. muddy the conceptual nature of the interface with implementational detail
  4. be like an abstract class with a private method (which is not allowed)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文