一整天都在阅读接口(interface)和抽象类,试图掌握它们,以更好地理解我正在使用的亚马逊库。我有这个代码:
using MWSClientCsRuntime;
namespace MarketplaceWebServiceOrders.Model
{
public interface IMWSResponse : IMwsObject
{
ResponseHeaderMetadata ResponseHeaderMetadata { get; set; }
}
和
namespace MWSClientCsRuntime
{
public interface IMwsObject
{
void ReadFragmentFrom(IMwsReader r);
string ToXML();
string ToXMLFragment();
void WriteFragmentTo(IMwsWriter w);
void WriteTo(IMwsWriter w);
}
}
我的第一个问题是我认为接口(interface)不能包含字段,但是它们可以包含像ResponseHeaderMetadata
这样的属性?
其次,在我的主程序中我有这行代码:
IMWSResponse response = null;
response
稍后用于存储调用方法调用后亚马逊发回的信息。 但是将接口(interface)类型的变量设置为 null 背后的含义是什么?
另外,一个接口(interface)可以实现另一个接口(interface)吗?不仅类可以实现接口(interface),接口(interface)本身也可以实现?
最佳答案
属性可以出现在接口(interface)中,因为属性实际上是方法 - 使用 T GetSomeValue()
旁边void SetSomeValue(T value)
在其他语言中变得如此普遍,以至于 C# 将它们实现为属性。
将接口(interface)成员设置为null
背后的含义与将任何其他属性设置为 null
相同- 由于属性(property)的 set
访问器是一种方法,就像调用接口(interface)上的任何其他方法一样。什么null
意味着由实现决定。
接口(interface)不实现彼此,因为接口(interface)不能包含代码,因此不实现;接口(interface)继承允许一个人要求另一个接口(interface)中的一个接口(interface)。一个很大的例子是 IEnumerable<T>
,与 IEnumerable
密切相关它继承,因此意味着任何实现 IEnumerable<T>
的类还必须实现IEnumerable
.
关于c# - 了解 C# 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35047855/