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

Chapter 17 Classes

117. Classes

2A class is a data structure that can contain data members (constants and fields), function members (methods,

3properties, events, indexers, operators, instance constructors, destructors, and static constructors), and nested

4types. Class types support inheritance, a mechanism whereby a derived class can extend and specialize a

5base class.

617.1 Class declarations

7A class-declaration is a type-declaration (§16.6) that declares a new class.

8class-declaration:

9

attributesopt class-modifiersopt partialopt class identifier type-parameter-listopt

10

class-baseopt type-parameter-constraints-clausesopt class-body ;opt

11A class-declaration consists of an optional set of attributes (§24), followed by an optional set of class-

12modifiers (§17.1.1), followed by an optional partial modifier (§17.1.4), followed by the keyword class

13and an identifier that names the class, followed by an optional type-parameter-list (§26.1.1), followed by an

14optional class-base specification (§17.1.1.3), followed by an optional type-parameter-constraints-clauses

15(§26.7), followed by a class-body (§17.1.3), optionally followed by a semicolon.

16A class declaration shall not supply a type-parameter-constraints-clauses unless it also supplies a type-

17parameter-list.

18A class declaration that supplies a type-parameter-list is a generic class declaration (§26.1).

1917.1.1 Class modifiers

20A class-declaration can optionally include a sequence of class modifiers:

21class-modifiers:

22

class-modifier

23

class-modifiers class-modifier

24

class-modifier:

25

new

26

public

27

protected

28

internal

29

private

30

abstract

31

sealed

32static

33It is a compile-time error for the same modifier to appear multiple times in a class declaration.

34The new modifier is permitted on nested classes. It specifies that the class hides an inherited member by the

35same name, as described in §17.2.2. It is a compile-time error for the new modifier to appear on a class

36declaration that is not a nested class declaration.

37The public, protected, internal, and private modifiers control the accessibility of the class.

38Depending on the context in which the class declaration occurs, some of these modifiers might not be

39permitted (§10.5.1).

40When a partial type declaration (§17.1.4) includes an accessibility specification (via the public,

41protected, internal, and private modifiers), that specification shall agree with all other parts that

255

C# LANGUAGE SPECIFICATION

1include an accessibility specification. If no part of a partial type includes an accessibility specification, the

2type is given the appropriate default accessibility (§10.5.1).

3The abstract, sealed, and static modifiers are discussed in the following subclauses.

417.1.1.1 Abstract classes

5The abstract modifier is used to indicate that a class is incomplete and that it is intended to be used only

6as a base class. An abstract class differs from a non-abstract class in the following ways:

7An abstract class cannot be instantiated directly, and it is a compile-time error to use the new operator on

8an abstract class. While it is possible to have variables and values whose compile-time types are

9abstract, such variables and values will necessarily either be null or contain references to instances of

10non-abstract classes derived from the abstract types.

11An abstract class is permitted (but not required) to contain abstract members.

12An abstract class cannot be sealed.

13When a non-abstract class is derived from an abstract class, the non-abstract class shall include actual

14implementations of all inherited abstract members, thereby overriding those abstract members. [Example: In

15the following code

16abstract class A

17{

18public abstract void F();

19}

20abstract class B: A

21{

22public void G() {}

23}

24class C: B

25{

26

public override void F() {

27

// actual implementation of F

28}

29}

30the abstract class A introduces an abstract method F. Class B introduces an additional method G, but since it

31doesn’t provide an implementation of F, B shall also be declared abstract. Class C overrides F and provides

32an actual implementation. Since there are no abstract members in C, C is permitted (but not required) to be

33non-abstract. end example]

34If one or more parts of a partial type declaration (§17.1.4) of a class include the abstract modifier, the

35class is abstract. Otherwise, the class is non-abstract.

3617.1.1.2 Sealed classes

37The sealed modifier is used to prevent derivation from a class. A compile-time error occurs if a sealed

38class is specified as the base class of another class.

39A sealed class cannot also be an abstract class.

40[Note: The sealed modifier is primarily used to prevent unintended derivation, but it also enables certain

41run-time optimizations. In particular, because a sealed class is known to never have any derived classes, it is

42possible to transform virtual function member invocations on sealed class instances into non-virtual

43invocations. end note]

44If one or more parts of a partial type declaration (§17.1.4) of a class include the sealed modifier, the class

45is sealed. Otherwise, the class is unsealed.

4617.1.1.3 Static classes

47When a class declaration includes a static modifier, the class being declared is said to be a static class.

256

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