c# - 需要有关我的界面代码的解释

标签 c# interface

任何人都可以解释下面的代码吗?任何人都可以用下面的代码片段给出一些实时解释

public interface roman
{
    roman this[string name] { get; }
}

public class notempty : roman
{
    public string Color{ get; set; }

    public roman this[string name]
    {
        get { return new notempty() { Color= "Value1" }; }
    }
}

最佳答案

public interface roman // there is an interface called "roman", accessible to all
{
    // all implementations of "roman" must have an "indexer" that takes a string
    // and returns another "roman" instance (it is not required to offer a "set")
    // typical usage:
    //     roman obj = ...
    //     roman anotherObj = obj["abc"];
    roman this[string name] { get; }
}

public class notempty : roman // there is a class "notempty", accessible to all,
{                             // which implements the "roman" interface

    // no constructors are declared, so there is a default public parameterless
    // constructor which simply calls the parameterless base-constructor

    // any instance of "notempty" has a string property called "Color" which can
    // be both read (get) and written (set) by any callers; there
    // is also a *field* for this, but the compiler is handling that for us
    public string Color{ get; set; }

    // there is an indexer that takes a string and returns a "roman"; since
    // there is no *explicit* implementation, this will also be used to satisfy
    // the "roman" indexer, aka "implicit interface implementation"
    public roman this[string name]
    {
        // when the indexer is invoked, the string parameter is disregarded; a
        // new "notempty" instance is created via the parameterless constructor,
        // and the "Color" property is assigned the string "Value1"; this is then
        // returned as "roman", which it is known to implement
        get { return new notempty() { Color= "Value1" }; }
    }
}

关于c# - 需要有关我的界面代码的解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8167743/

相关文章:

C# 如何测试文件是否为 jpeg?

c# - 如何使用服务器模式在 GridView 中获取异常详细信息?

java - 错误+继承+接口(interface)

c# - 类中没有泛型引用的泛型类型方法

pointers - 存储为值的接口(interface);无法更新结构字段的方法

c# - .net 并行和串行循环

c# - 间歇性System.ComponentModel.Win32Exception : The network path was not found

Java 面向对象编程多个类实例

c# - 使用 DI 的单一方法委托(delegate) vs 接口(interface)

c# - 应用程序关闭时将信息存储到文件的正确方法