[ Pobierz całość w formacie PDF ]

13.4.3 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
Copyright Microsoft Corporation 1999-2000. All Rights Reserved. 229
C# LANGUAGE REFERENCE
Control c = new Control();
TextBox t = new TextBox();
IControl ic = c;
IControl it = t;
c.Paint(); // invokes Control.Paint();
t.Paint(); // invokes TextBox.Paint();
ic.Paint(); // invokes Control.Paint();
it.Paint(); // invokes Control.Paint();
However, when an interface method is mapped onto a virtual method in a class, it is possible for derived classes
to override the virtual method and alter the implementation of the interface. For example, rewriting the
declarations above to
interface IControl
{
void Paint();
}
class Control: IControl
{
public virtual void Paint() {...}
}
class TextBox: Control
{
public override void Paint() {...}
}
the following effects will now be observed
Control c = new Control();
TextBox t = new TextBox();
IControl ic = c;
IControl it = t;
c.Paint(); // invokes Control.Paint();
t.Paint(); // invokes TextBox.Paint();
ic.Paint(); // invokes Control.Paint();
it.Paint(); // invokes TextBox.Paint();
Since explicit interface member implementations cannot be declared virtual, it is not possible to override an
explicit interface member implementation. It is however perfectly valid for an explicit interface member
implementation to call another method, and that other method can be declared virtual to allow derived classes to
override it. For example
interface IControl
{
void Paint();
}
class Control: IControl
{
void IControl.Paint() { PaintControl(); }
protected virtual void PaintControl() {...}
}
class TextBox: Control
{
protected override void PaintControl() {...}
}
Here, classes derived from Control can specialize the implementation of IControl.Paint by overriding the
PaintControl method.
230 Copyright Microsoft Corporation 1999-2000. All Rights Reserved.
Chapter 13 Interfaces
13.4.4 Interface re-implementation
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.
Inherited public member declarations and inherited explicit interface member declarations participate in the
interface mapping process for re-implemented interfaces. For example
interface IMethods
{
void F();
void G();
void H();
void I();
}
class Base: IMethods
{
void IMethods.F() {}
void IMethods.G() {}
public void H() {}
public void I() {}
}
class Derived: Base, IMethods
{
public void F() {}
void IMethods.H() {}
}
Here, the implementation of IMethods in Derived maps the interface methods onto Derived.F,
Base.IMethods.G, Derived.IMethods.H, and Base.I.
When a class implements an interface, it implicitly also implements all of the interface s base interfaces.
Likewise, a re-implementation of an interface is also implicitly a re-implementation of all of the interface s base
interfaces. For example
interface IBase
{
void F();
}
Copyright Microsoft Corporation 1999-2000. All Rights Reserved. 231
C# LANGUAGE REFERENCE
interface IDerived: IBase
{
void G();
}
class C: IDerived
{
void IBase.F() {...}
void IDerived.G() {...}
}
class D: C, IDerived
{
public void F() {...}
public void G() {...}
}
Here, the re-implementation of IDerived also re-implements IBase, mapping IBase.F onto D.F.
13.4.5 Abstract classes and interfaces
Like a non-abstract class, an abstract class must provide implementations of all members of the interfaces that
are listed in the base class list of the class. However, an abstract class is permitted to map interface methods onto
abstract methods. For example
interface IMethods
{
void F();
void G();
}
abstract class C: IMethods
{
public abstract void F();
public abstract void G();
}
Here, the implementation of IMethods maps F and G onto abstract methods, which must be overridden in non-
abstract classes that derive from C.
Note that explicit interface member implementations cannot be abstract, but explicit interface member
implementations are of course permitted to call abstract methods. For example
interface IMethods
{
void F();
void G();
}
abstract class C: IMethods
{
void IExample.F() { FF(); }
void IExample.G() { GG(); }
protected abstract void FF();
protected abstract void GG();
}
Here, non-abstract classes that derive from C would be required to override FF and GG, thus providing the actual
implementation of IMethods.
232 Copyright Microsoft Corporation 1999-2000. All Rights Reserved.
Chapter 14 Enums
14. Enums
An enum type is a distinct type with named constants. Enum declarations may appear in the same places that
class declarations can occur.
The example
using System;
enum Color
{
Red,
Green,
Blue
}
declares an enum type named Color with members Red, Green, and Blue.
14.1 Enum declarations
An enum declaration declares a new enum type. An enum declaration begins with the keyword enum, and
defines the name, accessibility, underlying type, and members of the enum.
enum-declaration:
attributesopt enum-modifiersopt enum identifier enum-baseopt enum-body ;opt
enum-modifiers:
enum-modifier
enum-modifiers enum-modifier
enum-modifier:
new
public
protected
internal
private
enum-base:
: integral-type
enum-body:
{ enum-member-declarationsopt }
{ enum-member-declarations , }
Each enum type has a corresponding integral type called the underlying type of the enum type. This underlying [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • loko1482.xlx.pl