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

CHAPTER 4 CLASSES: THE BASICS

Instance Members

A class declaration acts as a blueprint from which you can create as many instances of the class as you like.

Instance members: Each instance of a class is a separate entity that has its own set of data members, distinct from the other instances of the same class. These are called instance members since they are associated with an instance of the class.

Static members: Instance members are the default, but you can also declare members called static members that are associated with the class, rather than the instance. I’ll cover these in Chapter 6.

As an example of instance members, the following code shows the poker program with three instances of class Player. Figure 4-4 shows that each instance has a different value for the Name field.

class Dealer { ... }

 

 

 

 

 

// Declare class

class Player {

 

 

 

 

 

// Declare class

string Name;

 

 

 

 

 

// Field

...

 

 

 

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

 

 

 

class Program {

 

 

 

 

 

 

 

 

 

 

static void Main()

 

 

 

 

 

 

 

 

 

 

{

 

 

 

 

 

 

 

 

 

 

Dealer theDealer

= new Dealer();

Player player1

= new Player();

Player player2

= new Player();

Player player3

= new Player();

...

 

 

 

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 4-4. Instance members have distinct values between class objects.

59

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