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

class CritViewer

{

static void Main(string[] args)

{

CritViewer cv = new CritViewer(); } // end main

public CritViewer(){ //Create some critters

Critter alpha = new Critter("alpha", 10, 10, 0); Critter beta = new Critter("beta");

Critter charlie = new Critter();

//Make 'em talk Console.WriteLine(alpha.talk()); Console.WriteLine(beta.talk()); Console.WriteLine(charlie.talk()); Console.ReadLine();

} // end constructor

} // end CritViewer class

}// end namespace

The program works correctly, as you can see in Figure 5.7.

Figure 5.7: Although not apparent from the output, each critter uses a different constructor. The last one begins with a blank name because it was called with no parameters.

Using Inheritance to Make New Classes

The other major concept I introduced at the beginning of this chapter is inheritance. Like many computing words, inheritance is borrowed from the nontechnical world, but computing types added new significance to the term. The basic idea of inheritance in object−oriented programming is similar to the genetic meanings of inheritance: You have characteristics of your parents and your grandparents. Objects also have a family tree. Nearly every object in C# has exactly one parent. That parent might have a parent, and this class might also have a parent. A class can inherit characteristics from each member of its family tree, just as you might have your grandmother’s nose and your mother’s eyes. Inheritance in C# is much simpler than in genetics, however, because each class has only one parent.

Trap Some OOP languages (such as C++) allow an object to have more than one parent class, but multiple inheritance causes many more problems than it solves. C# uses an inheritance model that is simpler and less prone to error.

111

Creating a Class to View the Clone

To illustrate inheritance, I’ll show you the simplest example of inheritance I know.

Figure 5.8 illustrates the output of another version of the Critter program. From the output, this looks very much like the other programs you have seen in this chapter. However, when you look at the code that creates the Clone class, you’ll find a surprise.

Figure 5.8: The output looks very familiar, but Dolly is actually a clone!

Note that the code for CritViewer is also familiar:

using System;

namespace CritClone

{

///<summary>

///Another Critter Viewer

///This one demonstrates inheritance by making a clone

///Andy Harris, 12/21/01

///</summary>

class CritViewer

{

static void Main(string[] args)

{

CritViewer cv = new CritViewer(); } // end main

public CritViewer(){

Clone myClone = new Clone(); myClone.name = "Dolly"; Console.WriteLine(myClone.talk());

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

Console.ReadLine();

} // end constructor

} // end CritViewer class

}// end namespace

This program never directly invokes the Critter class. Instead, it makes an instance of the Clone class. The clone acts much like a critter. When you type it into the editor, you get the same list of properties as when you are working directly with a critter. Also, the clone has a talk() method that works just like the Critter class.

112

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