PART I
C h a p t e r 8 :
A C l o s e r L o o k a t M e t h o d s a n d C l a s s e s
187
PART IPART I
MyClass anotherOb = ob.Factory(i, j); // make an object
anotherOb.Show();
}
Console.WriteLine();
}
}
The output is shown here:
a and b: 0 10
a and b: 1 9
a and b: 2 8
a and b: 3 7
a and b: 4 6
a and b: 5 5
a and b: 6 4
a and b: 7 3
a and b: 8 2
a and b: 9 1
Let’s look closely at this example.
MyClass
does not define a constructor, so only the
default constructor is available. Thus, it is not possible to set the values of
a
and
b
using a
constructor. However, the class factory
Factory( )
can create objects in which
a
and
b
are
given values. Moreover, since
a
and
b
are private, using
Factory( )
is the only way to set
these values.
In
Main( )
, a
MyClass
object is instantiated, and its factory method is used inside the
for
loop to create ten other objects. The line of code that creates objects is shown here:
MyClass anotherOb = ob.Factory(i, j); // get an object
With each iteration, an object reference called
anotherOb
is created, and it is assigned a
reference to the object constructed by the factory. At the end of each iteration of the loop,
anotherOb
goes out of scope, and the object to which it refers is recycled.
Return an Array
Since in C# arrays are implemented as objects, a method can also return an array. (This
differs from C++ in which arrays are not valid as return types.) For example, in the
following program, the method
Factors(_)'>FindFactors( )
returns an array that holds the factors
of the argument that it is passed:
// Return an array.
using System;
class Factor {
/* Return an array containing the factors of num.
On return, numfactors will contain the number of
factors found. */
public int[] FindFactors(int num, out int numfactors) {
int[] facts = new int[80]; // size of 80 is arbitrary
int i, j;
www.freepdf-books.com
188
P a r t I :
T h e C # L a n g u a g e
// Find factors and put them in the facts array.
for(i=2, j=0; i < num/2 + 1; i++)
if( (num%i)==0 ) {
facts[j] = i;
j++;
}
numfactors = j;
return facts;
}
}
class FindFactors {
static void Main() {
Factor f = new Factor();
int numfactors;
int[] factors;
factors = f.FindFactors(1000, out numfactors);
Console.WriteLine("Factors for 1000 are: ");
for(int i=0; i < numfactors; i++)
Console.Write(factors[i] + " ");
Console.WriteLine();
}
}
The output is shown here:
Factors for 1000 are:
2 4 5 8 10 20 25 40 50 100 125 200 250 500
In
Factor
,
FindFactors( )
is declared like this:
public int[] FindFactors(int num, out int numfactors) {
Notice how the
int
array return type is specified. This syntax can be generalized. Whenever
a method returns an array, specify it in a similar fashion, adjusting the type and dimensions
as needed. For example, the following declares a method called
someMeth( )
that returns a
two-dimensional array of
double
:
public double[,] someMeth() { // ...
Method Overloading
In C#, two or more methods within the same class can share the same name, as long as
their parameter declarations are different. When this is the case, the methods are said to
be
overloaded,
and the process is referred to as
method overloading.
Method overloading is
one of the ways that C# implements polymorphism.
In general, to overload a method, simply declare different versions of it. The compiler
takes care of the rest. You must observe one important restriction: The type and/or number of
the parameters of each overloaded method must differ. It is not sufficient for two methods
to differ only in their return types. They must differ in the types or number of their parameters.
www.freepdf-books.com
Do'stlaringiz bilan baham: |