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

C# LANGUAGE SPECIFICATION

1class A

2{

3

public virtual void F() {

4

Console.WriteLine("A.F");

5

}

6

public virtual void G() {

7

Console.WriteLine("A.G");

8}

9}

10class B: A

11{

12

public sealed override void F() {

13

Console.WriteLine("B.F");

14

}

15

public override void G() {

16

Console.WriteLine("B.G");

17}

18}

19class C: B

20{

21

public override void G() {

22

Console.WriteLine("C.G");

23}

24}

25the class B provides two override methods: an F method that has the sealed modifier and a G method that

26does not. B’s use of the sealed modifier prevents C from further overriding F. end example]

2717.5.6 Abstract methods

28When an instance method declaration includes an abstract modifier, that method is said to be an abstract

29method. Although an abstract method is implicitly also a virtual method, it cannot have the modifier

30virtual.

31An abstract method declaration introduces a new virtual method but does not provide an implementation of

32that method. Instead, non-abstract derived classes are required to provide their own implementation by

33overriding that method. Because an abstract method provides no actual implementation, the method-body of

34an abstract method simply consists of a semicolon.

35Abstract method declarations are only permitted in abstract classes (§17.1.1.1).

36[Example: In the following code

37public abstract class Shape

38{

39public abstract void Paint(Graphics g, Rectangle r);

40}

41public class Ellipse: Shape

42{

43

public override void Paint(Graphics g, Rectangle r) {

44

g.DrawEllipse(r);

45}

46}

47public class Box: Shape

48{

49

public override void Paint(Graphics g, Rectangle r) {

50

g.DrawRect(r);

51}

52}

53the Shape class defines the abstract notion of a geometrical shape object that can paint itself. The Paint

54method is abstract because there is no meaningful default implementation. The Ellipse and Box classes

55are concrete Shape implementations. Because these classes are non-abstract, they are required to override

56the Paint method and provide an actual implementation. end example]

288

Chapter 17 Classes

1It is a compile-time error for a base-access (§14.5.8) to reference an abstract method. [Example: In the

2following code

3abstract class A

4{

5public abstract void F();

6}

7class B: A

8{

9

public override void F() {

 

10

base.F();

// Error, base.F is abstract

11}

12}

13a compile-time error is reported for the base.F() invocation because it references an abstract method. end

14example]

15An abstract method declaration is permitted to override a virtual method. This allows an abstract class to

16force re-implementation of the method in derived classes, and makes the original implementation of the

17method unavailable. [Example: In the following code

18using System;

19class A

20{

21

public virtual void F() {

22

Console.WriteLine("A.F");

23}

24}

25abstract class B: A

26{

27public abstract override void F();

28}

29class C: B

30{

31

public override void F() {

32

Console.WriteLine("C.F");

33}

34}

35class A declares a virtual method, class B overrides this method with an abstract method, and class C

36overrides that abstract method to provide its own implementation. end example]

3717.5.7 External methods

38When a method declaration includes an extern modifier, the method is said to be an external method.

39External methods are implemented externally, typically using a language other than C#. Because an external

40method declaration provides no actual implementation, the method-body of an external method simply

41consists of a semicolon.

42The mechanism by which linkage to an external method is achieved, is implementation-defined.

43[Example: The following example demonstrates the use of the extern modifier in combination with a

44DllImport attribute that specifies the name of the external library in which the method is implemented:

45using System.Text;

46using System.Security.Permissions;

47using System.Runtime.InteropServices;

48class Path

49{

50

[DllImport("kernel32", SetLastError=true)]

51

static extern bool CreateDirectory(string name, SecurityAttribute sa);

52

[DllImport("kernel32", SetLastError=true)]

53

static extern bool RemoveDirectory(string name);

289

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