Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

CSharp Precisely (2004) [eng]

.pdf
Скачиваний:
115
Добавлен:
16.08.2013
Размер:
3.22 Mб
Скачать

40 Classes

10.7 Method Declarations

A method must be declared inside a class (or struct, section 14). A method-declaration declaring method M has the form:

method-modifiers returntype M(formal-list) method-body

The formal-list is a comma-separated list of zero or more formal parameter declarations, each of one of these forms:

parameter-modifier type parametername params type[] parametername

The params form must appear last in a formal-list if at all; it can occur only once, and it cannot be used with the ref or out modifiers. A parameter-modifier may be out or ref or absent. A parameter that is neither an out parameter nor a ref parameter is a by-value parameter. The type is any type expression. The parametername is any name, but no two parameters in a formal-list can have the same name. The scope of a parameter is the method-body.

A by-value parameter is similar to a local variable, initialized with the value of a corresponding argument expression. An out parameter is similar to an uninitialized variable; and assignments to it immediately affects the corresponding argument variable. A ref parameter is similar to a variable initialized from the corresponding argument variable; and assignments to it immediately affects that argument variable. See section 12.15.2 for details on by-value, ref, and out parameter passing. Ref and out parameters can be used only with methods, constructors, delegates, and anonymous methods; not with properties, events, indexers, or operators.

The params modifier is used when declaring a method that can take a variable number of arguments. The formal params parameter in the method declaration must have array type; in a method call it gets bound to an array holding the values of zero or more arguments not bound to the preceding formal parameters. See also section 12.15.3. Parameter arrays cannot be used with anonymous methods.

The method name M together with the list t1, . . . , tn of declared parameter types in the formal-list determine the method signature M(t1, . . . , tn). The signature includes any ref and out modifiers, but not the params modifier (if any) and not the returntype.

A class may declare more than one method with the same name M, provided they have different signatures. This is called overloading of the method name. Overloaded methods may have different return types.

The method-body is a block-statement (section 13.2). In particular, the method-body may contain return statements. If the returntype is void, then the method does not return a value, and no return statement in the method-body can have an expression argument. If the returntype is not void, it must be a type and the method must return a value: it must not be possible for execution to reach the end of method-body without executing a return statement. Moreover, every return statement must have an expression argument whose type is implicitly convertible to the returntype.

The legal method-modifiers consist of a member access modifier as described in section 10.3 and a method kind modifier as described in section 10.8.

In a class whose class-base-clause includes interface I, the method name in a method declaration may be written I.M to explicitly state that it implements a method from interface I; in that case, there can be no method-modifiers. See section 15.3.

Team-LRN

Classes 41

Example 49 Method Name Overloading and Signatures

This class declares four overloaded methods M whose signatures are M(int) and M(bool) and M(int, double) and M(double, double). Some of the overloaded methods are static, others are instance methods. The overloaded methods may have different return types, as shown here. Example 82 explains the method calls.

It would be legal to declare an additional method with signature M(double, int), but then the method call M(10, 20) would become ambiguous and illegal. Namely, there is no best signature among

M(double, double) and M(int, double) or M(double, int); see section 12.15.1.

public class Overloading { double M(int i) { return i; } bool M(bool b) { return !b; }

static double M(int x, double y) { return x + y + 1; } static double M(double x, double y) { return x + y + 3; }

public static void Main(String[] args) {

 

Console.WriteLine(M(10, 20));

// Prints 31

Console.WriteLine(M(10, 20.0));

// Prints 31

Console.WriteLine(M(10.0, 20));

// Prints 33

Console.WriteLine(M(10.0, 20.0));

// Prints 33

}

 

}

 

Example 50 Method Overriding

In the vessel hierarchy (example 42), the classes Tank and Barrel override the method ToString inherited from the universal base class Object, and class Cube overrides ToString inherited from class Tank.

Example 51 Parameter Arrays and Overloading

This example declares three overloads of the Max method, including one with a parameter array (params). When the params-overload is called, the first argument will be bound to parameter x0 of Max, and all remaining arguments will be stored in a new array, bound to the parameter xr. Example 85 shows several calls to these methods. Example 132 demonstrates a parameter array in an indexer.

public static int Max(int a, double b) { return a > b ? a : (int) b;

}

public static int Max(int a, int b, int c) { a = a > b ? a : b;

return a > c ? a : c;

}

public static int Max(int x0, params int[] xr) { foreach (int i in xr)

if (i > x0) x0 = i;

return x0;

}

Team-LRN

42 Classes

10.8 Method Modifiers static, new, virtual, override, sealed, abstract

The method-modifiers are used in method declarations (section 10.7), property declarations (section 10.13), and indexer declarations (section 10.14). See also section 10.3 for member access modifiers. The following method-modifiers are legal (in classes C, structs S, or interfaces I):

Method Modifier

Meaning for This Method

Allowable Base Class Method

In

static

static method

no method, or private

CS-

new static

static method; hide base

any method

CS-

 

 

 

 

(none)

non-virtual instance method

no method, or private

CS-

new

non-virtual instance method; hide

any method

CSI

 

 

 

 

virtual

virtual method

no method, or private

C--

override

virtual; override base

virtual/override/abstract

CS-

sealed override

virtual; prevent further overriding

virtual or abstract

C--

new virtual

virtual; hide base

any method

C--

abstract

virtual and abstract: no body

no method, or private

C--

abstract override

virtual and abstract; override base

virtual or abstract

C--

new abstract

virtual and abstract; hide base

any method

C--

When a method’s accessibility (section 10.3) is not explicitly specified, it defaults to private, which is not permitted for methods that are abstract, virtual or override. There are several kinds of methods:

A static method is a method M (in a class or struct type C) that is declared static. Then M is associated with the class itself; it can be called without any object of class C. The method may be called as C.M(...) or, inside methods, constructors, field initializers, initializer blocks and nested classes and structs in C, simply as M(...). A static method cannot use the current object reference this and can refer only to static members of the enclosing class.

An instance method is a method M (in a class or struct type C) that is not declared static. Then every call to M is associated with an instance of class C or a subclass. Outside the class, the method must be called as o.M(...) where o is an object of class C or a subclass, or, inside instance methods and constructors C, simply as M(...) which has the same meaning as this.M(...). An instance method can refer to all members of class C, static or not. There are two kinds of instance methods:

A non-virtual instance method is one not declared virtual or override. In a call o.M(...) to such a method, only the compile-time type of o determines which method is called; see section 12.15.5.

A virtual instance method is one declared virtual or override. In a call o.M(...) to such a method, the compile-time type of o and the run-time class of the object o together determine which method is called; see section 12.15.5. Only class types can have virtual instance methods; struct types (section 14) cannot. A sealed method cannot be overridden in subclasses. The combination sealed virtual is illegal; that would just mean non-virtual.

A abstract method is a method M (in a class C) declared abstract. Then class C must be abstract too, and so cannot be instantiated. The declaration of an abstract method has no method body:

abstract method-modifiers returntype M(formal-list);

Team-LRN

Classes 43

Example 52 Method Inheritance, Hiding and Overriding

The abstract class A declares static method M1, non-virtual instance method M2, virtual method M3, and abstract virtual method M4. Class B overrides M4 and inherits the other ones. Class C hides the static M1 by a new static method M1 and hides non-virtual M2 by a new non-virtual M2, and overrides the virtual M3. Abstract class D hides non-virtual method M2 and virtual method M3 and overrides M3 by an abstract method. Class E overrides M2 and M4 with virtual methods. Calls to these methods are shown in example 87.

abstract

class A {

public

static void M1() { Console.Write("A.M1 "); }

public

void M2() { Console.Write("A.M2 "); }

public

virtual void M3() { Console.Write("A.M3 "); }

public

abstract void M4();

}

 

class B : A { public override void M4() { Console.Write("B.M4 "); } } class C : B {

public

new static void M1() { Console.Write("C.M1 "); }

public

new void

M2()

{ Console.Write("C.M2 "); }

public

override

void

M3() { Console.Write("C.M3 "); }

}

 

 

 

abstract

class D : C {

 

public

new abstract void M2();

public

new virtual void M3() { Console.Write("D.M3 "); }

public

abstract

override void M4();

}

 

 

 

class E : D {

 

 

public

sealed override void M2() { Console.Write("E.M2 "); }

public

override

void

M3() { Console.Write("E.M3 "); }

public

override

void

M4() { Console.Write("E.M4 "); }

}

 

 

 

Example 53 Static Field Initializers and the Static Constructor

The initializer of static field ps creates an array and binds it to ps. The static constructor (section 10.10) fills the array with an increasing sequence of pseudo-random numbers, then scales them so the last number is 1.0. This is useful for generating a random loaded die, and cannot be done using a static field initializer.

class InitializerExample {

static double[]

ps = new double[6];

 

static readonly

Random rnd =

new Random();

static InitializerExample() {

 

// Static constructor

double sum = 0;

 

 

for (int i=0;

i<ps.Length;

i++)

// Fill with increasing random numbers

ps[i] = sum

+= rnd.NextDouble();

// Random number 0 <= x < 1

for (int i=0;

i<ps.Length;

i++)

// Scale so last ps element is 1.0

ps[i] /= sum;

}

...

}

Team-LRN

44 Classes

10.9 Constructor Declarations

The purpose of an instance constructor in class C is to initialize a new object (instance) of the class. A constructor-declaration in class C has the form:

constructor-modifier C(formal-list) constructor-body

The constructor-modifier is one of the member access modifiers (section 10.3), and the formal-list is as for a method (section 10.7). A constructor cannot be abstract or sealed. For the static constructor, see section 10.10. A constructor has no return type.

Instance constructors may be overloaded in the same way as methods: the constructor signature (a list of the parameter types in formal-list) is used to distinguish instance constructors in the same class. A constructor may call another overloaded constructor in the same class using the syntax:

constructor-modifier C(formal-list) : this(actual-list) constructor-body

but a constructor may not call itself, directly or indirectly. The argument expressions in actual-list are static code and so cannot use the current object reference this or any instance member of class C, but can use the parameters from formal-list.

The constructor-body is a block-statement (section 13.2) and so may contain statements as well as declarations of variables. The constructor-body may contain return statements, but no return statement can take an expression argument.

A class which does not explicitly declare a constructor, instead implicitly declares a public, argumentless default constructor whose only action is to call the base class constructor (section 10.4):

public C() : base() { }

When new creates a new object in the computer store (section 12.9), a constructor is called to initialize the fields of the object. First the initializers of the object’s instance fields are executed once, in order of appearance in the class declaration. Then either another constructor this(...) in the same class or a base class constructor base(...) is called (explicitly or implicitly, see examples 55 and 88). Finally, the body of the constructor itself is executed. See example 56.

The call to a base class constructor will cause a call to a constructor in its base class, and execution of its instance field initializers, and so on, until reaching the constructor Object().

10.10 Static Field Initialization and the Static Constructor

A static field of an object or struct may be initialized by a static field initializer (section 10.5) or by a parameterless static constructor, declared as follows:

static C() block-statement

See example 53. All static field initializers are executed, in order of appearance in the class declaration, when the class is loaded. Then the body of the static constructor (if any) is executed. If the execution of a static field initializer or a static constructor throws an exception (section 19), then a TypeInitializationException is thrown instead, and the class is not loaded.

Team-LRN

Classes 45

Example 54 Constructor Overloading; Calling Another Constructor

We add a new constructor to the Point class (example 40), thus overloading its constructors. The old constructor has signature Point(int, int) and the new one Point(Point). The new constructor makes a copy of the point p by calling the old constructor using the syntax : this(p.x, p.y).

public class Point {

protected internal

int x, y;

 

public

Point(int x, int y)

// overloaded constructor

{ this.x = x; this.y = y; }

 

public

Point(Point

p)

// overloaded constructor

: this(p.x, p.y)

{}

// calls the first constructor

public

void Move(int dx, int dy)

 

{ x +=

dx; y += dy; }

 

public

override String ToString()

 

{ return "(" + x +

", " + y + ")"; }

 

}

Example 55 Calling a Baseclass Constructor

The constructor in the ColoredPoint subclass (example 130) calls its baseclass constructor using the syntax : base(x, y) in the header of the constructor declaration.

Example 56 The Execution Order of Field Initializers and Constructor Bodies

Evaluating new C() will execute the instance field initializers in class C, execute the instance field initializers in base class B, execute the body of the B() constructor, execute the body of the C(int) constructor, and finally execute the body of the C() constructor. That is, it will print 1 2 3 4 5 6 7.

class B {

public readonly int fb1 = Print(3); public B(int k) { Print(5); } public readonly int fb2 = Print(4);

public static int Print(int i) { Console.Write(i + " "); return i; }

}

class C : B {

public readonly int fc1 = Print(1); public C() : this(0) { Print(7); } public C(int k) : base(k) { Print(6); } public readonly int fc2 = Print(2);

}

Team-LRN

46 Classes

10.11 Member Classes or Nested Classes

A nested class is a class declared inside another class or struct type declaration. The code of a nested class may refer to all static members, even private ones, of the enclosing class or struct type, but cannot refer to its instance members except through a reference to an instance of the enclosing class or struct type. In contrast to Java, C# has no notion of an inner class, whose instances would contain a reference to an instance of the enclosing class.

A member name used in a nested class C denotes a member from the innermost enclosing class that declares or inherits a member of that name. That is, if class C has base class B and is declared as a nested class in E, then name resolution in C prefers members declared in C or (non-private) members inherited from base class B over members of the enclosing class E; see example 80.

A nested class may have static members as well as instance members.

10.12 Class Access Modifiers

The class-modifiers (section 10.1) may include an access modifier to control the accessibility of the class. For a class declared at top-level in a compilation unit or at top-level inside a namespace (section 25), the access modifier may be public or internal or absent (which means internal). A public toplevel class is accessible in all assemblies; an internal top-level class is accessible only in the assembly

(section 1.1) to which it belongs.

For a class declared inside a class or struct type, the access modifier may be one of the member access modifiers; see section 10.3.

Team-LRN

Classes 47

Example 57 An Enumerator As a Member Class

The member class SeqEnumerator, declared inside struct Seq from example 61, supports enumeration of the elements of a sequence using a foreach statement (section 13.6.2). The SeqEnumerator class must have a property Current to get the current sequence element and a method MoveNext to move to the next one. If it has also a method Reset, then it can implement interface IEnumerator (section 24.2), but this is not required for use in the foreach statement. In C# 2.0 an enumerator can be implemented more conveniently using the yield statement; see section 13.12 and example 123.

private class SeqEnumerator

: IEnumerator {

// Static member class

private readonly Seq seq;

 

 

private int i;

public SeqEnumerator(Seq seq) { this.seq = seq; Reset(); } public Object Current {

get {

if (0 <= i && i < seq.n) return seq.b + seq.k * i;

else

throw new InvalidOperationException();

}

}

public bool MoveNext() { i++;

return i < seq.n;

}

public void Reset() { i = -1; }

}

Example 58 Nested Classes Can Access All Static Members of Enclosing Class

The nested class C.D can access all static members of the enclosing class C, even private ones, as well as non-private static members of the base class B of the enclosing class. The enclosing class C cannot access the private members of a nested class C.D.

class B {

 

protected static int bx = 10;

 

private static int bz = 10;

 

}

 

class C : B {

 

private static int cx = 11;

 

public class D {

 

private static int dx = bx + cx;

// Can access protected bx and private cx

// public static int dz = bz + 2; // Cannot access private bz in base class

}

 

static void m() {

 

// int z = D.dx;

// Cannot access private dx in nested class

}

 

}

 

Team-LRN

48 Classes

10.13 Property Declarations

A property is used to get or set a value, using notations C.P and o.P and C.P=e and o.P=e similar to those for accessing or assigning a static or instance field; see section 12.16. Properties typically provide controlled access to private fields. A property may be read-only or write-only or read-write, according as it defines only a get-accessor or only a set-accessor or both. A declaration of the property P has the form

method-modifiers t P { get get-body

set set-body

}

The get-accessor or set-accessor, but not both, may be left out. The type t cannot be void. The methodmodifiers are as in section 10.7. A get-body is a block-statement which must return a value of type t. A set-body is a block-statement that cannot return a value. It is executed when an assignment P=e is performed, and can use the variable value of type t which initially holds the value of e.

The get-accessor of a property P of type t is a method t get_P(), the set-accessor is a method void set_P(t value), and the accessors follow the restrictions imposed on such methods. For instance, the get-body and set-body of an abstract property must consist of a semicolon (;) only. A class can declare any number of properties with different names. Properties cannot be overloaded.

A getor set-accessor can contain arbitrary code, but should have no unexpected side-effects and should be fast; otherwise the resemblance of a property to a variable could lead to (performance) bugs.

10.14 Indexer Declarations

An indexer is used to get or set a value, using notations C[i] and o[i] and C[i]=e and o[i]=e similar to those for accessing or assigning an array element; see section 12.17. Indexers typically provide controlled access to elements of collections. An indexer may be read-only or write-only or read-write, according as it defines a get-accessor or a set-accessor or both. A declaration of an indexer has the form

indexer-modifiers t this[formal-list] { get get-body

set set-body

}

The get-accessor or set-accessor, but not both, may be left out. The indexer-modifiers is a list of the modifiers abstract, extern, a legal combination of new, override, sealed and virtual (section 10.8), and a legal combination of access modifiers (section 10.3). An indexer cannot be static. The type t cannot be void. The formal-list is as for a method, but the list must be non-empty and cannot include ref or out parameters. A get-body is a block-statement which must return a value of type t; it can use the parameters in the formal-list. A set-body is a block-statement that cannot return a value. It can use the parameters in the formal-list, and the variable value of type t which initially holds the value of the right-hand side of an assignment to the indexer.

The getand set-accessors of an indexer this[formal-list] of type t are methods t get_Item(formallist) and void set_Item(t value, formal-list). A getor set-accessor can execute arbitrary code, but should have no unexpected side-effects and should be fast, as for properties.

Indexers can be overloaded, and different indexers may have different return types; see example 132.

Team-LRN

Classes 49

Example 59 Property Used to Access a Log

Class Log implements a simple log that stores the last SIZE strings written to it. The static property InstanceCount is the number of Log objects created. Property Count is the number of log entries written to this log. Property Last is the latest string logged to this log, if any. It has setas well as get-accessors, so one can use assignment operators such as log.Last += "...". Property All returns an array of the last (up to) five strings logged. Example 89 uses these properties.

public class Log {

private static int instanceCount = 0; private int count = 0;

private String[] log = new String[SIZE]; public Log() { instanceCount++; }

public static int InstanceCount { get { return instanceCount; } } public void Add(String msg) { log[count++ % SIZE] = msg; } public int Count { get { return count; } }

public String Last {

get { // Return the last log entry, or null if nothing logged yet return count==0 ? null : log[(count-1)%SIZE];

}

set { // Update the last log entry, or create one if nothing logged yet if (count==0)

log[count++] = value; else

log[(count-1)%SIZE] = value;

}

}

public String[] All { ... }

}

Example 60 Sparse Matrix Indexing

A sparse matrix is a matrix that has many zeroes. A sparse matrix may be represented as an array of columns, where each column is a list of only the non-zero elements. Using a two-argument indexer, one can get and set its elements as A[i,j], as if it were an ordinary rectangular array. However, the indexer performs a linear search of column j for the element with index i, so element access is not constant time.

public double this[int i, int j] { get {

NonZeroList colj = this[j]; int k = 0;

while (k < colj.Count && colj[k].i < i) k++;

return k < colj.Count && colj[k].i == i ? colj[k].Mij : 0.0;

}

set {

...

}

}

Team-LRN

Соседние файлы в предмете Программирование на C++