2011-11-17 153 views
2

谁能解释下面的代码可以在任何一个给下面一些代码实时解释网页摘要需要解释我的界面代码

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" }; } 
    } 
} 
+0

如果你解释这将有助于其位,你做的,不明白 – Justin

+1

实时间解释?你想要电话会议吗? –

+0

我建议你阅读这个.. http://stackoverflow.com/questions/2697783/what-does-program-to-interfaces-not-implementations-mean/2697810#2697810 –

回答

3
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" }; } 
    } 
}