Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft CSharp Programming For The Absolute Beginner (2002) [eng]-1.pdf
Скачиваний:
46
Добавлен:
16.08.2013
Размер:
15.71 Mб
Скачать

braces. However, you must isolate any code you think will cause problems so that you don’t end up trapping for the wrong error. The catch statement describes which type of exception you are expecting to encounter. The code in braces following the catch statement is the special code you run if an exception occurs. This typically involves informing the user that something went wrong and taking action to rectify the situation.

Returning a Value

The entire point of the showMenu() method is to get a numeric value from the user and return this value to the user. By the last line of the method, you are guaranteed to have an integer value in the input variable, so you return that value to whatever program called the showMenu() method.

Creating a New Object with the CritterName Program

You have begun to understand the benefits of OOP, but the real fun begins when you create your own objects. Although making the Critter program work without using OOP principles is possible, the point of this chapter is to show how to create objects, so that’s what you do next.

Figure 4.10 shows the output of the Critter Name program you create in this section.

Figure 4.10: This version of the Critter program features a critter that knows its name.

Creating the Basic Critter

A critter is an object. Objects, as you recall, can have properties, methods, and events. An object has a class (which is like a recipe—it defines the cookie but isn’t a cookie) and instances (the cookies made with the recipe). To create a critter object, you have to create a class and make an instance of that class. You can start with a very simple version of the Critter class. Later in the chapter you’ll give it more capabilities, such as changing its name and talking. Here’s the code for the simpler Critter class:

using System;

namespace CritterName {

///<summary>

///Critter Name

///Creating a simple class

///Andy Harris, 12/13/01

89

/// </summary>

class Menu {

static void Main(string[] args) {

...

} // end main

static int showMenu(){

...

} // end showMenu

}// end class

class Critter { public string name;

} // end class

}// end namespace

Note that I collapsed the Main() and showMenu() methods in this code listing—because there are very few changes in the code (I’ll show you those changes shortly) and because I wanted you to see the general structure of the program. For the first time, your program has more than one class in it. You can add as many classes to a project as you want. The Menu class is largely the same, but the Critter class is new. You create a new class with the class keyword, followed by the name of the class and a pair of braces ({ }). Classes do not have parentheses because they do not accept parameters.

The properties, methods, and events a class owns are collectively known as members. This version of the Critter class has only one member, name.

Using Scope Modifiers

A scope modifier is a special word used to determine whether a method or variable is visible to other methods and classes.

When you look back at the code for the Critter class above, the definition of the variable name is unique. It looks like most variable definitions, except that it starts with the keyword public. This term indicates that the variable name should be available to elements both inside and outside the Critter class. As you’ll see momentarily, this is not such a great idea, but I wanted to show you the simplest kind of class first.

The private keyword is an example of a scope modifier. C# supports a number of scope modifiers, but for now, you need to know only two. If you want a variable to be available outside the class, you declare it as public, as I did with name in this example. If you want the variable to exist only inside the class, you either declare the variable private or leave out a scope modifier (because the default scope is private).

Using a Public Instance Variable

Because the name variable is declared inside the Critter class and is used by instances of that class, it’s an example of an instance variable. Instance variables are variables defined inside a class but outside any method definitions. All this terminology seems overwhelming, but you’ll run across it later, so you had better learn the lingo. In any case, the formal description of name would be "a public instance variable of the Critter class." Right now, all the critter object has is this one public instance variable. This makes the class very simple to build, but it has some limitations. You’ll improve the Critter class throughout the chapter, but at least it works for now, so you can

90

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