简体   繁体   English

在C#中使用辅助功能修饰符继承属性

[英]Inheriting properties with accessibility modifier in C#

I tried to inherit interface, and make some of the automatically generated set property as private. 我试图继承接口,并将一些自动生成的set属性设置为private。 This is an example. 这是一个例子。

public class MyClass
{
    public interface A 
    {
        int X {get; set;}
    }
    public interface B : A
    {
        int Y {get; set;}
    }

    public class C : A
    {
        public int X {get; private set;}
    }

When I tried to compile it. 当我试图编译它。 I got an error 'MyClass.C' does not implement interface member 'MyClass.AXset'. 'MyClass.CXset' is not public. 我收到错误'MyClass.C' does not implement interface member 'MyClass.AXset'. 'MyClass.CXset' is not public. 'MyClass.C' does not implement interface member 'MyClass.AXset'. 'MyClass.CXset' is not public. .

I tried with private set; 我试过private set; in iterface A , but I got this error again : 'MyClass.AXset': accessibility modifiers may not be used on accessors in an interface . iterface A ,但我又遇到了这个错误: 'MyClass.AXset': accessibility modifiers may not be used on accessors in an interface

Is this accessibility modifier not allowed in C#? C#中不允许使用此辅助功能修饰符吗?

I tried with private set; 我试过私人套装; in iterface A, but I got this error again 在iterface A中,但我又遇到了这个错误

If your interface only requires that a property should be retrievable, you define it as: 如果您的接口仅要求可检索属性,则将其定义为:

public interface A 
{
    int X {get;}  // Leave off set entirely
}

The declaration of an interface defines the public set of members that the implementing type must have. 接口声明定义了实现类型必须具有的公共成员集。 So, if C implements A , it must have a public member for each member defined by the interface. 因此,如果C实现A ,它必须为接口定义的每个成员都有一个公共成员。

A defines that any implementing type must have a public property X with a public getter and a public setter. A定义任何实现类型必须具有带有公共getter和public setter的公共属性X C does not meet this requirement. C不符合此要求。

You can think of an interface as the minimum functionality that your class must implement. 您可以将接口视为类必须实现的最小功能。 If the interface specifies that a property exposes a get and a set clause, then you must implement a public get and set clause in your class, because only public methods and properties can implicitly implement interfaces. 如果接口指定属性公开getset子句,那么必须在类中实现public getset子句,因为只有公共方法和属性才能隐式实现接口。

You can simply leave out the set keyword in the interface property definition if you don't want to expose a public mutator. 如果您不想公开公共mutator,可以简单地在接口属性定义中省略set关键字。 Then you can make the implementation mutator either public or private. 然后,您可以将实现mutator设为public或private。

No it is not allowed. 不,不允许。 Remember, code which is using an instance of class C must be able to treat it as an interface A , which means that the contract is a public getter and setter for property X. 请记住,使用class C实例的代码必须能够将其视为interface A ,这意味着合同是属性X的公共getter和setter。

This applies to class inheritance as well as interface inheritance -- you must follow the contract of the type you are derived from. 这适用于类继承以及接口继承 - 您必须遵循派生类型的契约。

If the intent of the code is that property X should not have a public setter, then the interface should be defined with just the { get; } 如果代码的意图是属性X不应该有公共setter,那么接口应该只用{ get; } { get; }

I believe interface members must be public if the interface itself is public. 我相信如果界面本身是公共的,界面成员必须是公共的。 Your implementation of the property is faulty because of that. 因此,您对该属性的实现是错误的。

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

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