13.4.4 Interface mapping
A class or struct must provide implementations of all members of the interfaces that are listed in the base class list of the class or struct. The process of locating implementations of interface members in an implementing class or struct is known as interface mapping.
Interface mapping for a class or struct C
locates an implementation for each member of each interface specified in the base class list of C
. The implementation of a particular interface member I.M
, where I
is the interface in which the member M
is declared, is determined by examining each class or struct S
, starting with C
and repeating for each successive base class of C
, until a match is located:
- If
S
contains a declaration of an explicit interface member implementation that matches I
and M
, then this member is the implementation of I.M
.
- Otherwise, if
S
contains a declaration of a non-static public member that matches M, then this member is the implementation of I.M
. If more than one member matches, it is unspecified which member is the implementation of I.M
. This situation can only occur if S
is a constructed type where the two members as declared in the generic type have different signatures, but the type arguments make their signatures identical.
...
13.4.5 Interface implementation inheritance
A class inherits all interface implementations provided by its base classes.
Without explicitly re-implementing an interface, a derived class cannot in any way alter the interface mappings it inherits from its base classes. For example, in the declarations
interface IControl
{
void Paint();
}
class Control: IControl
{
public void Paint() {...}
}
class TextBox: Control
{
new public void Paint() {...}
}
the Paint
method in TextBox
hides the Paint
method in Control
, but it does not alter the mapping of Control.Paint
onto IControl.Paint
, and calls to Paint
through class instances and interface instances will have the following effects
Control c = new Control();
TextBox t = new TextBox();
IControl ic = c;
IControl it = t;
c.Paint();
t.Paint();
ic.Paint();
it.Paint();
...
13.4.6 Interface reimplementation
A class that inherits an interface implementation is permitted to re-implement the interface by including it in the base class list.
A re-implementation of an interface follows exactly the same interface mapping rules as an initial implementation of an interface. Thus, the inherited interface mapping has no effect whatsoever on the interface mapping established for the re-implementation of the interface. For example, in the declarations
interface IControl
{
void Paint();
}
class Control: IControl
{
void IControl.Paint() {...}
}
class MyControl: Control, IControl
{
public void Paint() {}
}
the fact that Control
maps IControl.Paint
onto Control.IControl.Paint
doesn’t affect the re-implementation in MyControl
, which maps IControl.Paint
onto MyControl.Paint
.
public new string Name { get; }
对于Base
上的Name
做了什么吗? - James ThorpeIBase o = new Derived();
中,编译器有两个选择,它会选择最佳匹配。 - H H