Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 4 CLASSES: THE BASICS

Accessing Members from Inside the Class

As mentioned, members of a class can access the other class members by just using their names. For example, the following class declaration shows the methods of the class accessing the fields

and other methods. Even though the fields and two of the methods are declared private, all the members of a class can be accessed by any method (or any function member) of the class. Figure 4-7 illustrates the code.

class DaysTemp

 

 

 

 

 

 

 

 

 

 

{

 

 

 

 

 

 

 

 

 

 

 

 

// Fields

 

 

 

 

 

 

 

 

 

 

private int High

= 75;

 

 

 

 

 

 

 

 

private int Low

= 45;

 

 

 

 

 

 

 

 

// Methods

 

 

 

 

 

 

 

 

 

 

private int GetHigh()

 

 

 

{

return High;

 

 

 

 

 

 

 

// Access private field

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

private int GetLow()

 

 

 

{

return Low;

 

 

 

 

 

 

 

// Access private field

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

public float Average ()

 

 

 

{

return (GetHigh() + GetLow()) / 2;

// Access private methods

 

}

 

 

 

 

 

 

 

}

Accessing the private methods

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 4-7. Members within a class can freely access each other.

63

CHAPTER 4 CLASSES: THE BASICS

Accessing Members from Outside the Class

To access a public instance member from outside the class, you must include the variable name and the member name, separated by a period (dot). This is called dot-syntax notation; it will be discussed in more detail later.

For example, the second line of the following code shows an example of accessing a method from outside the class:

DaysTemp myDt =

new DaysTemp();

//

Create

an

object of the

class.

float fValue =

myDt.Average();

//

Access

it

from outside.

 

 

 

 

 

 

 

 

 

Variable name

Member name

 

 

 

 

 

As an example, the following code declares two classes: DaysTemp and Program.

The two fields in DaysTemp are declared public, so they can be accessed from outside the class.

Method Main is a member of class Program. It creates a variable and object of class DaysTemp, and it assigns values to the fields of the object. It then reads the values of the fields and prints them out.

class DaysTemp

// Declare class DaysTemp

{

 

 

public int

High = 75;

public int

Low

= 45;

}

 

 

class Program

 

// Declare class Program.

{

 

 

static void Main()

{

 

Variable name

 

 

 

 

 

 

 

 

 

 

 

 

 

DaysTemp temp = new DaysTemp();

// Create the object.

Variable name and field

 

 

 

 

 

 

 

 

 

 

 

 

temp.High = 85;

 

 

// Assign to the fields.

 

temp.Low = 60;

Variable name and field

 

 

 

 

 

 

 

 

 

 

Console.WriteLine("High:

{0}", temp.High );

// Read from fields.

}

Console.WriteLine("Low:

{0}", temp.Low

);

 

 

 

 

 

 

 

 

 

}

This code produces the following output:

High: 85

Low: 60

64

CHAPTER 4 CLASSES: THE BASICS

Putting It All Together

The following code creates two instances and stores their references in variables named t1 and t2. Figure 4-8 illustrates t1 and t2 in memory. The code demonstrates the following three actions discussed so far in the use of a class:

Declaring a class

Creating instances of the class

Accessing the class members (that is, writing to a field and reading from a field)

class DaysTemp

// Declare the class.

{

 

public int High, Low;

// Declare the instance fields.

public int Average()

// Declare the instance method.

{

 

return (High + Low) / 2;

 

}

 

}

 

class Program

 

{

 

static void Main()

 

{

 

//Create two instances of DaysTemp. DaysTemp t1 = new DaysTemp(); DaysTemp t2 = new DaysTemp();

//Write to the fields of each instance.

t1.High

=

76;

t1.Low

=

57;

t2.High

=

75;

t2.Low

=

53;

//Read from the fields of each instance and call a method of

//each instance.

 

Console.WriteLine("t1: {0}, {1}, {2}",

 

 

 

t1.High, t1.Low, t1.Average() );

 

Console.WriteLine("t2: {0}, {1}, {2}",

 

 

 

t2.High, t2.Low, t2.Average() );

 

 

 

 

 

}

 

Field

Field

Method

}

65

CHAPTER 4 CLASSES: THE BASICS

This code produces the following output:

t1: 76, 57, 66 t2: 75, 53, 64

Figure 4-8. Memory layout of instances t1 and t2

66

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]