.

Web.com Web Hosting
Sign up for PayPal and start accepting credit card payments instantly.

.

Hello If you are new here, so you might want to subscribe for my feed so Subscribe in a reader. Or bring updates In Your Email

Saturday, July 17, 2010

Introduction to Classes in C# Tutorial

Previously I introduced you Objects in C#. Today I'll give you the introduction of Classes in C#

Look at the world around you. It is full of objects of various shapes, sizes, colors and behavior. For example,  the earth is inhabited by millions of animals. A zoologist cannot do any meaningful study of these objects without organizing this vast variety of animals found in the world in a logical manner. Thus, the zoologists have classified animals into kingdoms, genus, families and species.

All animals and birds, which are essentially objects, can be classified on the basis of their common attributes. For example, the peacock, the sparrow and the kingfisher are all birds. All of them share characteristics that are common to the family of birds. All of them lay eggs, are covered with feathers, have hollow bone structures and have the ability to fly. Therefore, they share structural and behavioral similarities and belong to the class called Birds.


You may define a class as a declaration, a template or a blueprint that can be used to classify objects. Similarly, Book is a class and 'Gone with the Wind' and 'Farewell to Arms' are the objects of this class.

Consider a video game that involves two players, Tom and Mark, who fight with each other. Tom chooses a shotgun and Mark chooses a pistol as their weapons in the game. If you need to create an object model for this game, you can identify a total of four objects. They are Tom, Mark, the shotgun and the pistol.

The object shotgun belongs to the class Firearms. It has certain attributes that are common with the pistol. However a shotgun is a unique object. Both the shotgun and the pistol are examples or instances of Firearms. Therefore, the shotgun and pistol are objects of the same class.

Messages and Methods

Objects do not exist in isolation. they interact with other objects. These interactions take place through messages. Grady Booch has defined behavior as follows:

"Behavior is how an object acts and reacts, in terms of its state changes and message passing."

In the example of a video game, every time Tom attacks, Mark either attacks or defends himself.

When Tom attacks Mark, Mark receives a message and reacts to that message. The reaction can be exhibiting a particuler behavior such as running, hiding, dodging or fighting back. For example, when Tom attacks, Mark receives a message "Being Attacked". Mark responds to this message by hiding. In this situation, hiding is a behavior exhibited by mark. Therefore, behavior (also called method in object-oriented methodology is simply a set of actions taken by the receiving object in response to a message.

No comments:

Post a Comment

Related Posts with Thumbnails