Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АрхитектураБиологияГеографияДругоеИностранные языки
ИнформатикаИсторияКультураЛитератураМатематика
МедицинаМеханикаОбразованиеОхрана трудаПедагогика
ПолитикаПравоПрограммированиеПсихологияРелигия
СоциологияСпортСтроительствоФизикаФилософия
ФинансыХимияЭкологияЭкономикаЭлектроника

Introduction to objects

Programs and algorithms | Algorithms and programming languages | Fundamentals of programming I | Student is obliged to write down and run this program by himself | Data in a program (variables and literals) | Operations on data (operators and expressions) | Fundamentals of programming II | Write down and run this program on your computer | Types. Primitive types. | Types of variables. Declarations. |


Читайте также:
  1. After method: an introduction
  2. An Introduction to Oxford University
  3. Application using an introduction
  4. AUTHOR'S INTRODUCTION TO WHITE LIGHT/WHITE HEAT: THE VELVET UNDERGROUND DAY-BY-DAY
  5. Brief Introduction to the Turkmen Language
  6. Case Study – Introductions
  7. Ch.1 - Introduction

Object-oriented languages are based on the concepts of an object and a class.
Precise definitions of these terms will be presented later. For now, instead of concentrating on abstract formulas, let us activate our imagination remembering that some notions will be fully explained further in the course.

So what is an "object"? Intuitively we feel that it is a kind of thing which may be isolated, named and characterized by its properties.
For instance we may consider the following objects: a bicycle, a car, a dog or a man.

Each of these objects has different properties. A man has a name and age. A car has a colour and may be characterized by the power of engine or the number of doors.
Two different cars have the same set of properties (attributes): a make, a colour or the power of engine. Although makes, colours and engine powers may differ, the cars are similar in a sense (as they are characterized by the same set of properties). We say that the cars are objects of the same class.
A class is a description of invariable properties of a group of similar entities.

Furthermore, note that an object can perform some operations. So other objects can make requests to that object, asking it to perform some operation on itself. For instance a driver-object may ask a car-object to pull out or to stop.

We say that a message is sent to an object requesting it to perform some operation.

Objects cannot accept arbitrary messages: a car-object can start or stop but it cannot sing.
The set of messages understood (accepted) by an object is its characteristic too.

Thus a class does not only describe the common properties of a group of similar objects such as colour, age or weight but also the set of messages accepted (understood) by the objects of this class.

The above reasoning is an abstraction of the real world.

Having such facilities incorporated into a programming language, we could easily translate real-world problems into programs.
This is realized in object-oriented languages. It is a very important characteristic which simplifies programming.

Let us consider a class of electrical devices with the following properties: width, height, state (on-off) and satisfying the requests of switching on and off.
We try to model this situation in a programming language. The messages accepted by an object of our class are implemented as methods (a method is a kind of function).

class ElDev { width, height; <-- attributes: width, height, state isOn;================= messages method on() isOn = true; <--- the request on (switch the device on) method off() isOn = false; <--- the request off (switch the device off)}

Let us assume we have two objects of the above class - electrical devices - denoted by a and b. We can simulate a sequence of actions: switch on the device a, switch on the device b, switch off the device a, switch off the device b with the help of messages sent to those objects (in other words: by invoking the methods for objects). For example, using the Java (or C++) syntax we could note the above sequence of requests as follows:

a.on(); // message sent to the object a requesting it to switch onb.on(); // let b switches on a.off(); // let a switches off

Besides reflecting the problem in the programming language, the object-oriented approach has one more advantage.

Usually object's attributes are not directly available. In a program objects talk to each other with the help of messages which manipulate their attributes.

The object's data (attributes) should be hidden and should be accessed only by means of messages sent to the object.

Such an approach is called encapsulation and allows for making more reliable programs.

Object-oriented languages allow for reusing existing classes by creating new ones. It simplifies programming and protects against mistakes.
It reflects real-world situations too.

For example, a computer is an electric appliance. So it is an object of the class ElDev. But computers have their specific characteristics besides those mentioned in the definition of the class ElDev (width, height, state).
Thus they constitute a subclass of the class representing electrical devices.
In a programming language we reflect it by means of inheritance. A class can inherit attributes and methods from some other class adding some more specialized ones.
As an example let us create a class Computer which inherits from the class ElDev. We do not have to write down all the properties and messages of an electrical device (we do not have to implement methods on() and off()). But we may add properties that are characteristic to a computer - for instance the request of executing some program.

Programs are objects of the class Program. A message sent to a computer requesting execution of a program should pass the program as an argument.
The inheritance is expressed using the keyword extends. So our new class Computer which inherits from the ElDev and adds a new method run can be written down thus:

class Computer extends ElDev { method run(Program p) if (isOn()) execution_of_the_program_p;}

It may be used in a program as follows:


Дата добавления: 2015-11-16; просмотров: 59 | Нарушение авторских прав


<== предыдущая страница | следующая страница ==>
Write down and run this program on your computer| The first program

mybiblioteka.su - 2015-2024 год. (0.007 сек.)