C# Tutorial - Learning to Read

posted Jun 10, 2011, 10:04 AM by admin@ revitviet.vn

Everyday we use items in our home, school or workplace, each of these is an object. One of the first books I remember reading involved a 'boy' named 'Peter', a 'girl' named 'Jane', a 'dog' and a 'ball' that was 'red'. So in terms of software we can use these things to also learn about objects.

If we read the sentence again, we can use the nouns to suggest candidate objects to use in a software program. Each of the items in bold are objects, the other items in quotations are information about an object. There is a distinction between what an object is, and what is only information about an object.

A 'boy' named 'Peter', a 'girl' named 'Jane', a 'dog' and a 'ball' that was 'red'.

An object is often a tangible item, something that can be seen, touched or felt, or something that can be eluded to, conceptualised, thought about. This is a good starting place when thinking about them. Objects can be...

  • Physical real world items.
  • Abstract items such as a bank account or dates.
  • Interactive software abstractions such as windows, buttons or applications.
  • Programming language constructs such as strings, arrays and I/O streams.

Abstraction

So we have a 'boy' object, a 'girl' object, a 'dog' object and a 'ball' object. Let's first consider the 'boy' object, we do not really want to think of a single item when developing objects, but instead we think about a "type" of object. In this case we do not think about 'Peter', but instead we think about a 'boy'. This "type" of object is defined as a class in C# and other object-oriented programming languages.

But what about girls? If we have a boy object, we might also want a girl object. This seems a bit of a silly thing to do as boys and girls, men and women are all people. So maybe we should think of them as 'person' objects.

What is an Object?

Alan Kay summarized the five basic characteristics of Smalltalk, the first successful object-oriented language and one of the languages upon which C# is based. These characteristics represent a pure approach to object-oriented programming:

  • Everything is an object. Think of an object as a fancy variable; it stores data, but you can "make requests" to that object, asking it to perform operations on itself. In theory, you can take any conceptual component in the problem you're trying to solve (dogs, buildings, services, etc.) and represent it as an object in your program.
  • A program is a bunch of objects telling each other what to do by sending messages. To make a request of an object, you "send a message" to that object. More concretely, you can think of a message as a request to call a method that belongs to a particular object.
  • Each object has its own memory made up of other objects. Put another way, you create a new kind of object by making a package containing existing objects. Thus, you can build complexity into a program while hiding it behind the simplicity of objects.
  • Every object has a type. Using the parlance, each object is an instance of a class, in which "class" is synonymous with "type". The most important distinguishing characteristic of a class is "What messages can you send to it?"
  • All objects of a particular type can receive the same messages. This is actually a loaded statement, because an object of one type may also be an object of another type. For example, a circle is also a shape and is therefore guaranteed to accept shape messages. This means you can write code that talks to shapes and automatically handle anything that fits the description of a shape. This substitutability is one of the powerful concepts in OOP.

Objects can be...

  • Physical real world items
  • Abstract items such as a bank account or dates
  • Interactive software abstractions such as windows, buttons or applications
  • Programming language constructs such as strings, arrays and I/O streams
Comments