3.2 Classes and ObjectsThe most important metaphors in object-oriented programming are the class and the object. A class defines a new type of thing. The class defines the common characteristics of every object of that new type. For example, you might define a class Car. Every car will share certain characteristics (wheels, brake, accelerator, and so forth). Your car and my car both belong to the class of Cars; they are of type Car. An object is an individual instance of a class. Each individual car (your particular car, my particular car) is an instance of the class Car, and thus is an object. An object is just a thing. We perceive the world to be composed of things. Look at your computer. You do not see various bits of plastic and glass amorphously merging with the surrounding environment. You naturally and inevitably see distinct things: a computer, a keyboard, a monitor, speakers, pens, paper. Things. More importantly, even before you decide to do it, you've categorized these things. You immediately classify the computer on your desk as a specific instance of a type of thing: this computer is one of the type computer. This pen is an instance of a more general type of thing, pens. It is so natural you can't avoid it, and yet the process is so subtle it's difficult to articulate. When I see my dog Milo, I can't help also seeing him as a dog, not just as an individual entity. Milo is an instance, Dog is a class. The theory behind object-oriented programming is that for computer programs to accurately model the world, the programs should reflect this human tendency to think about individual things and types of things. In C# you do that by creating a class to define a type and creating an object to model a thing. |