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

Static methods can be useful, but they have a serious limitation: Static methods cannot refer to instance variables because the instance variables have meaning only in an instance. The Main() method must be declared static, because it is the first entry into the program from the operating system. To avoid some of these limitations of static methods, the Main() method is usually much simpler than it has been in the examples you have seen so far in this book.

That sounds complicated, but it isn’t. The keyword static can also be read as class−level. The keyword instance in the preceding code refers to instance−level. Recall the cookie recipe analogy. A recipe is a class, and the cookies are instances of that class. A static method belongs to the entire class, not to a specific instance. In other words, a static method is a method that can be applied to the class, but not necessarily to the instances of that class. A recipe class may have copy and e−mail methods. An instance of a cookie is not required to invoke the class−level methods. It doesn’t make sense for e−mailing to belong to cookies (the instances of the class). (Besides, cookie dough is very hard on floppy drives.) E−mailing is a method of the recipe (the class itself), so Cookie.email() would most likely be a static method. The e−mail method is not concerned with actual cookie instances, so it makes sense that the static method would not have access to the details of individual cookies. Because you are e−mailing the recipe, not a cookie, the e−mail method shouldn’t have access to the bitesMissing property of a cookie because bitesMissing is an instance−level property (as most properties are).

Calling a Constructor from the Main() Method

The Main() method has to be a static method because it is called from the operating system. When you run a C# program, the first thing the operating system does is look for a Main() method. That Main() method runs before any specific classes are instantiated. Usually, it calls a constructor or two to get things started. A Main() method rarely contains much more than one call to a constructor because the static limitations can get in the way. Instead, the Main() method usually creates an instance of a class or two. You might be surprised to see which class the CritViewer’s Main() method creates:

CritViewer cv = new CritViewer();

The Main() method of the CritViewer class creates an instance of the CritViewer class! This idea might seem like the work of the department of redundancy department, but it makes sense in terms of static methods. Main() is a static method, which means that it runs before an instance of the CritViewer object occurs. Because I really want an instance of the object here, I use the Main() method to create an instance of the class. Objects with a Main() method often utilize this technique to pull themselves into existence. When you run a program that contains an object with a Main() method, that Main() method runs before any other code executes. The main method calls the class’ s constructor, which finishes creating the class.

Trick If more than one class has a Main() method, you can set the properties of the project in the project window to determine which object’s Main() method will start the program.

Examining CritViewer’s Constructor

The rest of the work in the CritViewer class occurs in its constructor:

//This next method is the constructor for CritViewer public CritViewer(){

Critter myCritter = new Critter("alpha", 10, 10, 0); Console.WriteLine("I'm in critViewer"); Console.WriteLine(myCritter.talk());

106

Console.WriteLine();

Console.WriteLine("Please press Enter key to continue"); Console.ReadLine();

} // end constructor

Constructors are instance−level because they create an instance of an object. The constructor of the critter viewer creates an instance of the Critter class. I took advantage of the critter’s newfound constructor capabilities. Building the critter with this four−parameter constructor is convenient because it saves you a lot of typing time.

Working with Multiple Files

Until now, all the source code in your programs has existed in a single file on the disk. This is fine for small programs but becomes unwieldy when your programs are longer. After you start building programs with multiple classes, you should open a new file for every class. The default file that loads in the editor when you start a program already has a Main() method. You usually use that program as the container class to hold your other classes. Figure 5.5 demonstrates how to create a new class in the Visual Studio editor.

Figure 5.5: To create a new class, choose Add Class from the Project menu, then click Class.

The Visual Studio IDE enables you to have many files open at the same time. This can be very handy when your programs become complex. As a default, the new class will belong to the same namespace as your existing classes because all the classes in your project are meant to work together.

The IDE also has a class viewer feature for looking at all the parts of your object. The Class View, illustrated in Figure 5.6, is usually available in the right segment of the editor.

107

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