Answer the question
In order to leave comments, you need to log in
What to read and what to practice on, I can’t move from procedural to oop?
I started learning languages from ZX Basic and ASM, then Pascal and Delphi.
Then I went to the administration and I write all the working scripts, even on PowerShell / PowerCli, according to the principle of procedural programming, I use OOP only when I work with the interface, unless of course it can be called OOP.
Now I'm studying python and ruby, and so far the basics, everything seems to be easy and understandable, but how it comes to objects is all a stupor.
Tell me where to start?
Answer the question
In order to leave comments, you need to log in
Bertrand Meyer - Object-Oriented Design of Software Systems
Matt Weisfeld - Object-Oriented Thinking
Grady Booch - Object-Oriented Analysis and Design with Sample Applications
I really understood OOP when I stopped understanding the code written an hour ago during the completion of the program.
I realized that without it in any way, I refactored what was at that time. Later, for another year and a half, I sawed that program, but there were no problems with readability. This is the question of why it is needed.
Only practice will help you learn. Try not to write interfaces and other high-level stuff for now. Take something simple. For example, write your own library for working with linear algebra.
2 classes - matrix and vector. And work with them. Input, output, multiplication, transposition, search for matrix determinant.
Add static methods, such as identity matrix generation.
Then you can write the solutions of the SLE in different ways. Think about how to reuse ready-made methods.
There will be inheritance.
Try writing your own simple ORM. A set of basic CRUD methods as an abstract class, and classes inheriting from it to work with specific tables.
If you do not get carried away with perfectionism, and the universality of the use of these classes, these 2 bicycles will be quite enough to figure out what's what with OOP in any language you choose.
Read five pages from Java Philosophy, where at the beginning Eckel, as far as I remember, correctly analyzed this issue. I remember it helped me about 8 years ago)
Read from the second page:
www.rulit.me/books/filosofiya-java-read-180907-2.html
Do not be afraid of the word Java, first explanations in isolation from languages.
Practice. Solve problems without letting yourself write a line outside of objects. The theory is dry...
It's OK. I will say this - just try to solve the problem in a different way. If we take the C language as an example, there is a defined data type, a structure (in Psacal, this is sort of like a "record" is called).
That is, there is a data type with which we can put data about some subject of discussion - say, films: year, rating, name of the main actor, etc. It is important to understand the point itself here - just like you create an integer, you describe a data structure/records and wrap it in your derived type, but you say - "Hey compiler, I need not a number, a character, I need to create my new data type, inside it will be 3 integers and 1 string; then create 3 data object of my type.". T e you spawned a new data type. This is more convenient than running after a bunch of variables and figuring out how they relate to each other, now the information about !each! The movie is stored in its arranged location.
And let's go further - in languages where there is coop, you can make a class - data + functions. This is a kind of new data type containing not only values, but also code (the same functions). In other words, we created a structure and stuffed functions into each object in order to store code in each object that can work with data. This is the class, on the basis of it you create new objects. What is the advantage? Let's take our films. For example, it will be very convenient to call a function from the object that leads the dialog for entering information about the movie and we will not miss anything - title, year, actor, rating. The object will not be created until we enter full information about it. Or let's say it will have a function where it will consider the rating as a failure if it is below 5. We don't need to pull the object every time, it analyzes the data itself and writes information to it.
I think that the theory of OOP does not make sense to read. Here is the same Python - Bill Lubanovich "Python. Modern programming style". Try it, it's written simply. In Python, everything is an object (in the book, from the first chapters, all this is shown and there is nothing terrible there). The most practical task is to connect to zabbix and try to get data out of it. Feel the OOP right away. The scheme is simple and stupid - you import the module for working with zabbix, then you initialize the object (zabbix host, login, password). And EVERYTHING! you have an object, you work through it, everything is already in it (if authentication failed or it is impossible to connect to the host, then the object will not be created, here is an OOP example for you, that the object has a code in addition to data). Or another example - the same Lubanovich has an example with creating a config file. (Config Parser). little has been written about him, stupidly googling. In short - the import of the module (some guy wrote it), and you just import the module where this "data type" (class) already exists, and on the basis of it you create objects and work with them. And then add the task to yourself - to collect data from objects into a heap and process them. This is where you will write the classes =)
I, at one time, was helped by the study of design patterns (patterns). I liked how Matt Zandstra presented this topic in the book "PHP: Objects, Patterns and Programming Techniques", starting from chapter 7.
but as it comes to objects, everything is a stupor. Tell me where to start?
Now OOP is everywhere, it is better to look at the implementation of OOP in Python and Ruby ... You can also capture Java, there without objects there is nowhere at all ...
Well, it's very simple. An object is an entity that has some state and behavior through which this state can be changed. An object can contain other objects, in which case it is called an aggregate.
Let's say you're writing software for a trucking company. The company probably at least has a car and a load. The cargo can be placed in the car. It is impossible to place the volume of cargo more than the capacity of the car.
You can model this example in php (the syntax may be incorrect!) in this way
(it is desirable to make the properties of objects private with access through getters so that the state of the object cannot be changed outside of it. Omitted, for the sake of simplicity).
class Car {
public $id;
public $capacity;
public $payload;
public function __construct(int $id, int $capacity, array $payload) {
$this->id = $id;
$this->capacity = $capacity;
$this->payload = $payload;
}
public function assignCargo(Cargo $cargo) {
if ($cargo->volume + $this->calculateOccupiedCapacity() > $this->capacity) {
throw new DomainException('The capacity has been exceeded.');
}
$this->payload[] = $cargo;
}
private function calculateOccupiedCapacity() {
return array_reduce($this->payload, function($accumulator, $cargo) {
return $accumulator += $cargo->volume
});
}
}
class Cargo {
public $volume;
public function __construct($volume) {
$this->volume = $volume;
}
}
An object is some data (first of all), and methods that work with this data.
We are moving away from the fact that any data is in any global variables and come to the conclusion that all data is in variables inside the object. So access to them outside - through methods. Change is through methods. This is where the object comes in.
You need to understand why OOP is needed. It is not needed for small projects. In projects on which more than 1-3 people are working, it is already useful. And in large ones, it is necessary, otherwise, in principle, it will not be possible to organize the work of a large number of people on one program, without encapsulation, for which OOP was invented.
You can try to show the "principle of maximum harm" in training - deliberately put yourself somewhere in strict conditions that require the use of OOP.
For example, you can start writing very short - from a dozen lines - useful at work programs on smalltalk (or self or Io ).
For example - to write something like simple log analyzers for calculating statistics - in the Smoltok library, for example, there are regular expressions and, in general, classes for working with text - which will become the basis of many of their text bicycles.
It's also useful to remember that OOP came out of procedural programming and you can look at modern implementations of various procedural languages - for example, openeuphoria .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question