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

Chapter 17 Classes

1protected C(): base() {}

2or

3public C(): base() {}

4where C is the name of the class. end note]

5[Example: In the following code

6class Message

7{

8

object sender;

9string text;

10}

11a default constructor is provided because the class contains no instance constructor declarations. Thus, the

12example is precisely equivalent to

13class Message

14{

15

object

sender;

16

string

text;

17public Message(): base() {}

18}

19end example]

2017.10.5 Private constructors

21When a class declares only private instance constructors, it is not possible for other classes (that are not

22nested in the class) to derive from that class or to create instances of that class. [Example: Private instance

23constructors can be used to restrict instantiation of the class to a prescribed set of instances. For example:

24public class Color

25{

26

private byte val;

27

private static Color red;

28

private static Color green;

29

private static Color blue;

30

private Color(byte val) { this.val = val; }

31

public static Color Red

32

{

33

get { if (red == null) red = new Color(0); return red; }

34

}

35

public static Color Green

36

{

37

get { if (green == null) green = new Color(1); return green; }

38

}

39

public static Color Blue

40

{

41

get { if (blue == null) blue = new Color(2); return blue; }

42}

43}

44Since Color’s constructor is private, there will never be more than three instances of the Color class,

45namely the ones stored in the static fields and returned by the static properties. end example]

46At least one instance constructor shall be declared to suppress the automatic generation of a default

47constructor.

4817.10.6 Optional instance constructor parameters

49[Note: The this(…) form of constructor initializer is commonly used in conjunction with overloading to

50implement optional instance constructor parameters. In the example

315

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