单独的 getter 和 setter 声明
Separate getter and setter declarations
如何为 属性 分别声明 getter 和 setter?
例如,假设我要创建以下层次结构:
interface IReadOnlyFoo
{
string Value { get; }
}
interface IFoo : IReadOnlyFoo
{
string Value { set; }
}
class BasicFoo : IFoo
{
string Value { get; set; }
}
编译器正在抱怨,因为 IFoo.Value
隐藏了 IReadOnlyFoo.Value
,这不是我想要做的。我想要 "merge" getter 和 setter 声明。
我看过 .NET Framwork 如何声明 IReadOnlyList
和 IList
接口,但它是以不同的方式完成的。
我怎样才能实现我想做的事情?我可以使用 属性 来做到这一点,还是我真的必须创建单独的 GetValue()
和 SetValue()
方法?
当您将接口定义更改为
interface IReadOnlyFoo
{
string Value { get; }
}
interface IReadWriteFoo
{
string Value { get; set; }
}
class BasicFoo : IFoo, IReadOnlyFoo
{
public string Value { get; set; }
}
应该可以。
当您实现接口时,这两个成员将被合并,因为您在 IFoo.Value 中没有 get 方法。
interface IReadOnlyFoo
{
string Value { get; }
}
interface IFoo : IReadOnlyFoo
{
new string Value { set; }
}
class BasicFoo : IFoo
{
public string Value { get; set; }
}
只要您对接口使用隐式实现,它就会按照您的预期运行。另一方面,如果您希望接口的成员有两种不同的行为,那么您需要使用显式实现。你可以在这里找到一个例子
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/how-to-explicitly-implement-members-of-two-interfaces
您只需在代码中更改 getter 到 IFoo
界面中的 Value
属性。
从语义上讲,IFoo
是一种特定的 IReadOnlyFoo
,它为其基础类型添加了另一种功能(Value
属性 的 Setter ).
这是面向对象编程中继承的确切定义 - 子类型是其基类型的更具体版本,并为其添加功能。
interface IReadOnlyFoo
{
string Value { get; }
}
interface IFoo : IReadOnlyFoo
{
new string Value { get; set; }
}
class BasicFoo : IFoo
{
public string Value { get; set; }
}
此代码完全有效,将为您提供所需的信息。
这样,如果您有一个 IReadOnlyFoo
类型的引用到 BasicFoo
的一个实例,那么 Value
属性 确实是只读的,但是如果您的引用类型是 IFoo
是读/写 属性.
如何为 属性 分别声明 getter 和 setter?
例如,假设我要创建以下层次结构:
interface IReadOnlyFoo
{
string Value { get; }
}
interface IFoo : IReadOnlyFoo
{
string Value { set; }
}
class BasicFoo : IFoo
{
string Value { get; set; }
}
编译器正在抱怨,因为 IFoo.Value
隐藏了 IReadOnlyFoo.Value
,这不是我想要做的。我想要 "merge" getter 和 setter 声明。
我看过 .NET Framwork 如何声明 IReadOnlyList
和 IList
接口,但它是以不同的方式完成的。
我怎样才能实现我想做的事情?我可以使用 属性 来做到这一点,还是我真的必须创建单独的 GetValue()
和 SetValue()
方法?
当您将接口定义更改为
interface IReadOnlyFoo
{
string Value { get; }
}
interface IReadWriteFoo
{
string Value { get; set; }
}
class BasicFoo : IFoo, IReadOnlyFoo
{
public string Value { get; set; }
}
应该可以。
当您实现接口时,这两个成员将被合并,因为您在 IFoo.Value 中没有 get 方法。
interface IReadOnlyFoo
{
string Value { get; }
}
interface IFoo : IReadOnlyFoo
{
new string Value { set; }
}
class BasicFoo : IFoo
{
public string Value { get; set; }
}
只要您对接口使用隐式实现,它就会按照您的预期运行。另一方面,如果您希望接口的成员有两种不同的行为,那么您需要使用显式实现。你可以在这里找到一个例子
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/how-to-explicitly-implement-members-of-two-interfaces
您只需在代码中更改 getter 到 IFoo
界面中的 Value
属性。
从语义上讲,IFoo
是一种特定的 IReadOnlyFoo
,它为其基础类型添加了另一种功能(Value
属性 的 Setter ).
这是面向对象编程中继承的确切定义 - 子类型是其基类型的更具体版本,并为其添加功能。
interface IReadOnlyFoo
{
string Value { get; }
}
interface IFoo : IReadOnlyFoo
{
new string Value { get; set; }
}
class BasicFoo : IFoo
{
public string Value { get; set; }
}
此代码完全有效,将为您提供所需的信息。
这样,如果您有一个 IReadOnlyFoo
类型的引用到 BasicFoo
的一个实例,那么 Value
属性 确实是只读的,但是如果您的引用类型是 IFoo
是读/写 属性.