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

 

Chapter 17 Classes

1

public int Count {

2

get { return 5; }

3

set { }

4}

5}

6class B: A

7{

8

private string text = "goodbye";

9

private int count = 0;

10

new public string Text {

11

get { return text; }

12

protected set { text = value; }

13

}

14

new protected int Count {

15

get { return count; }

16

set { count = value; }

17}

18}

19class M

20{

21

static void Main() {

 

22

B b = new B();

 

23

b.Count = 12;

// Calls A.Count set accessor

24

int i = b.Count;

// Calls A.Count get accessor

25

b.Text = "howdy";

// Error, B.Text set accessor not accessible

26

string s = b.Text;

// Calls B.Text get accessor

27}

28}

29end example]

30An accessor that is used to implement an interface cannot have an accessor-modifier. However, if only one

31accessor is used to implement an interface, the other accessor can be declared with an accessor-modifier:

32[Example:

33public interface I

34{

35string Prop { get; }

36}

37public class C: I

38{

39

public string Prop {

 

40

get { return "April"; } // Shall not have a modifier here

41

internal set {…}

// Ok, because I.Prop has no set accessor

42}

43}

44end example]

4517.6.3 Virtual, sealed, override, and abstract accessors

46A virtual property declaration specifies that the accessors of the property are virtual. The virtual

47modifier applies to all non-private accessors of a property. When an accessor of a virtual property has the

48private accessor-modifier, the private accessor is implicitly not virtual.

49An abstract property declaration specifies that the accessors of the property are virtual, but does not

50provide an actual implementation of the accessors. Instead, non-abstract derived classes are required to

51provide their own implementation for the accessors by overriding the property. Because an accessor for an

52abstract property declaration provides no actual implementation, its accessor-body simply consists of a

53semicolon. An abstract property shall not have a private accessor.

54A property declaration that includes both the abstract and override modifiers specifies that the property

55is abstract and overrides a base property. The accessors of such a property are also abstract.

297

C# LANGUAGE SPECIFICATION

1Abstract property declarations are only permitted in abstract classes (§17.1.1.1). The accessors of an

2inherited virtual property can be overridden in a derived class by including a property declaration that

3specifies an override directive. This is known as an overriding property declaration. An overriding

4property declaration does not declare a new property. Instead, it simply specializes the implementations of

5the accessors of an existing virtual property.

6An overriding property declaration shall specify the exact same accessibility modifiers, type, and name as

7the inherited property. If the inherited property has only a single accessor (i.e., if the inherited property is

8read-only or write-only), the overriding property shall include only that accessor. If the inherited property

9includes both accessors (i.e., if the inherited property is read-write), the overriding property can include

10either a single accessor or both accessors. If one of the inherited accessors is not accessible to the overriding

11type, the overriding property shall not include that accessor. In addition, the declared accessibility of the

12accessors shall match that of the overridden accessors. [Example:For example:

13public class B

14{

15

public virtual int P {

16

protected set {…}

17

get {…}

18

}

19

public virtual int Q {

20

private set {…} // not virtual

21

get {…}

22}

23}

24public class D: B

25{

26

public override int P {

 

27

protected set {…}

// Must specify protected here

28

get {…}

// Must not have a modifier here

29

}

 

30

public override int Q {

 

31

private set {…}

// Error: inherited set is not accessible

32

get {…}

// Must not have a modifier here

33}

34}

35end example]

36An overriding property declaration can include the sealed modifier. Use of this modifier prevents a

37derived class from further overriding the property. The accessors of a sealed property are also sealed.

38Except for differences in declaration and invocation syntax, virtual, sealed, override, and abstract accessors

39behave exactly like virtual, sealed, override and abstract methods. Specifically, the rules described in

40§17.5.3, §17.5.4, §17.5.5, and §17.5.6 apply as if accessors were methods of a corresponding form:

41A get accessor corresponds to a parameterless method with a return value of the property type and the

42same modifiers as the containing property.

43A set accessor corresponds to a method with a single value parameter of the property type, a void

44return type, and the same modifiers as the containing property.

45[Example: In the following code

46abstract class A

47{

48

int y;

49

public virtual int X {

50

get { return 0; }

51

}

52

public virtual int Y {

53

get { return y; }

54

set { y = value; }

55

}

298

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