Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C-sharp language specification.2004.pdf
Скачиваний:
14
Добавлен:
23.08.2013
Размер:
2.55 Mб
Скачать

C# LANGUAGE SPECIFICATION

1interface ITextBox: IControl

2{

3void SetText(string text);

4}

5interface IListBox: IControl

6{

7void SetItems(string[] items);

8}

9class ComboBox: IControl, ITextBox, IListBox

10{

11

void

IControl.Paint() {…}

12

void

ITextBox.SetText(string text) {…}

13void IListBox.SetItems(string[] items) {…}

14}

15it is not possible to have separate implementations for the IControl named in the base class list, the

16IControl inherited by ITextBox, and the IControl inherited by IListBox. Indeed, there is no notion of

17a separate identity for these interfaces. Rather, the implementations of ITextBox and IListBox share the

18same implementation of IControl, and ComboBox is simply considered to implement three interfaces,

19IControl, ITextBox, and IListBox. end example]

20The members of a base class participate in interface mapping. [Example: In the following code

21interface Interface1

22{

23void F();

24}

25class Class1

26{

27

public void F() {}

28public void G() {}

29}

30class Class2: Class1, Interface1

31{

32new public void G() {}

33}

34the method F in Class1 is used in Class2's implementation of Interface1. end example]

3520.4.3 Interface implementation inheritance

36A class inherits all interface implementations provided by its base classes.

37Without explicitly re-implementing an interface, a derived class cannot in any way alter the interface

38mappings it inherits from its base classes. [Example: In the declarations

39interface IControl

40{

41void Paint();

42}

43class Control: IControl

44{

45public void Paint() {…}

46}

47class TextBox: Control

48{

49new public void Paint() {…}

50}

51the Paint method in TextBox hides the Paint method in Control, but it does not alter the mapping of

52Control.Paint onto IControl.Paint, and calls to Paint through class instances and interface

53instances will have the following effects

346

Chapter 20 Interfaces

1Control c = new Control();

2TextBox t = new TextBox();

3IControl ic = c;

4IControl it = t;

5

c.Paint();

// invokes Control.Paint();

6

t.Paint();

// invokes TextBox.Paint();

7

ic.Paint();

// invokes Control.Paint();

8

it.Paint();

// invokes Control.Paint();

9end example]

10However, when an interface method is mapped onto a virtual method in a class, it is possible for derived

11classes to override the virtual method and alter the implementation of the interface. [Example: Rewriting the

12declarations above to

13interface IControl

14{

15void Paint();

16}

17class Control: IControl

18{

19public virtual void Paint() {…}

20}

21class TextBox: Control

22{

23public override void Paint() {…}

24}

25the following effects will now be observed

26Control c = new Control();

27TextBox t = new TextBox();

28IControl ic = c;

29IControl it = t;

30

c.Paint();

// invokes Control.Paint();

31

t.Paint();

// invokes TextBox.Paint();

32

ic.Paint();

// invokes Control.Paint();

33

it.Paint();

// invokes TextBox.Paint();

34end example]

35Since explicit interface member implementations cannot be declared virtual, it is not possible to override an

36explicit interface member implementation. However, it is perfectly valid for an explicit interface member

37implementation to call another method, and that other method can be declared virtual to allow derived

38classes to override it. [Example:

39interface IControl

40{

41void Paint();

42}

43class Control: IControl

44{

45

void IControl.Paint() { PaintControl(); }

46protected virtual void PaintControl() {…}

47}

48class TextBox: Control

49{

50protected override void PaintControl() {…}

51}

52Here, classes derived from Control can specialize the implementation of IControl.Paint by overriding

53the PaintControl method. end example]

347

C# LANGUAGE SPECIFICATION

120.4.4 Interface re-implementation

2A class that inherits an interface implementation is permitted to re-implement the interface by including it in

3the base class list.

4A re-implementation of an interface follows exactly the same interface mapping rules as an initial

5implementation of an interface. Thus, the inherited interface mapping has no effect whatsoever on the

6interface mapping established for the re-implementation of the interface. [Example: In the declarations

7interface IControl

8{

9void Paint();

10}

11class Control: IControl

12{

13void IControl.Paint() {…}

14}

15class MyControl: Control, IControl

16{

17public void Paint() {}

18}

19the fact that Control maps IControl.Paint onto Control.IControl.Paint doesn’t affect the re-

20implementation in MyControl, which maps IControl.Paint onto MyControl.Paint. end example]

21Inherited public member declarations and inherited explicit interface member declarations participate in the

22interface mapping process for re-implemented interfaces. [Example:

23interface IMethods

24{

25

void F();

26

void G();

27

void H();

28void I();

29}

30class Base: IMethods

31{

32

void IMethods.F() {}

33

void IMethods.G() {}

34

public void H() {}

35public void I() {}

36}

37class Derived: Base, IMethods

38{

39

public void F() {}

40void IMethods.H() {}

41}

42Here, the implementation of IMethods in Derived maps the interface methods onto Derived.F,

43Base.IMethods.G, Derived.IMethods.H, and Base.I. end example]

44When a class implements an interface, it implicitly also implements all of that interface’s base interfaces.

45Likewise, a re-implementation of an interface is also implicitly a re-implementation of all of the interface’s

46base interfaces. [Example:

47interface IBase

48{

49void F();

50}

51interface IDerived: IBase

52{

53void G();

54}

55class C: IDerived

56{

57

void IBase.F() {…}

348

Chapter 20 Interfaces

1void IDerived.G() {…}

2}

3class D: C, IDerived

4{

5

public void F() {…}

6public void G() {…}

7}

8Here, the re-implementation of IDerived also re-implements IBase, mapping IBase.F onto D.F. end

9example]

1020.4.5 Abstract classes and interfaces

11Like a non-abstract class, an abstract class shall provide implementations of all members of the interfaces

12that are listed in the base class list of the class. However, an abstract class is permitted to map interface

13methods onto abstract methods. [Example:

14interface IMethods

15{

16

void F();

17void G();

18}

19abstract class C: IMethods

20{

21

public abstract void F();

22public abstract void G();

23}

24Here, the implementation of IMethods maps F and G onto abstract methods, which shall be overridden in

25non-abstract classes that derive from C. end example]

26Explicit interface member implementations cannot be abstract, but explicit interface member

27implementations are of course permitted to call abstract methods. [Example:

28interface IMethods

29{

30

void F();

31void G();

32}

33abstract class C: IMethods

34{

35

void IMethods.F() { FF(); }

36

void IMethods.G() { GG(); }

37

protected abstract void FF();

38protected abstract void GG();

39}

40Here, non-abstract classes that derive from C would be required to override FF and GG, thus providing the

41actual implementation of IMethods. end example]

349

Соседние файлы в предмете Электротехника