任何人都可以解释下面的代码,任何人都可以用下面的代码片段给出一些实时解释
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 :(得分: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" }; }
}
}
答案 1 :(得分:2)
Inferface roman 定义所有实现都应该有一个索引器 这个[字符串名称] ,它返回一个罗马的实例。
退房:
C# Station Tutorial - Interfaces
和
C# Station Tutorial - Indexers
希望这会有所帮助