1. 程式人生 > >Object-Oriented Programming

Object-Oriented Programming

Object-oriented programming (OOP) is programming primarily focused on data, while data and behavior are being inseparably linked. Data and behavior together constitute a class, while object are class instances.

OOP consider computation as modeling of behavior. The modeled item is the object represented by computational abstractions. Suppose we want to write a well known game "Tetris". To do this, we must learn how to model the appearance of random shapes composed of four squares joined together by edges. Also we need to regulate the falling speed of shapes, define operations of rotation and shift of shapes. Moving of shapes is limited by the well's boundaries, this requirement must also be modeled. Besides that, filled rows of cubes must be destroyed and achieved points must be counted.

Thus, this easy to understand game requires the creation of several models - shape model, well model, shape movement model and so on. All these models are abstractions, represented by calculation in the computer. To describe these models, the concept of Abstract Data Type, ADT(or complex data type) is used. Strictly speaking, the model of "shapes" motion in DOM is not a data type, but it is a set of operations on the "shape" data type, using the restrictions of "well" data type.

Objects are class variables. Object-oriented programming allows you to easily create and use ADT. Object oriented programming uses the inheritance mechanism. The benefit of inheritance is the face that it allows obtaining derivative types from data types already defined by a user.

For example, to create Tetris shapes, it's convenient to create a base class Shape first. The other classes representing all seven possible shape types can be derived on its basis. Behavior of shapes is defined in the base class, while implementation of behavior of each separator shape is defined in derivative classes.

In OOP objects are responsible for their behavior. ADT developer should include a code to describe any behavior that would normally be expected from the corresponding objects. The fact that the object is responsible for its behavior, greatly simplifies the task of programming for the user of this object. If we want to draw a shape on the screen, we need to know where the center will be and how to draw it. if a separator shape know how to draw itself, the programmer should send a "draw" message when using such a shape.

The concept of OOP has a set of related concepts, including the following:

Simulated of actions from the real world

user-defined data types

Hiding the implementation details

Possibility of code reuse through inheritance

interpretation of function calls during execution

some of these concept are rather vague, some are abstract, others are general.