Home >Backend Development >PHP Tutorial >javascript - How to elegantly answer 'What is object-oriented'?
I found that many interviewers like to ask this question (I am a student), but every time I feel that my answer to this question is unsatisfactory.
My answer is usually
Its design features are: inheritance, encapsulation, polymorphism;
From concrete to abstract. Properties become variables and behaviors become methods;
However, the interviewer seems not very satisfied. I wonder if there are any relatively elegant answers from various experts?
I found that many interviewers like to ask this question (I am a student), but every time I feel that my answer to this question is unsatisfactory.
My answer is usually
Its design features are: inheritance, encapsulation, polymorphism;
From concrete to abstract. Properties become variables and behaviors become methods;
However, the interviewer seems not very satisfied. I wonder if there are any relatively elegant answers from various experts?
The first answer is a programming paradigm.
Then give a detailed description of the three features you mentioned, for example:
Inheritance: subclasses can reuse variables and methods exposed by the parent class;
Encapsulation: Shield a series of details. You only need to know the existence of this method when calling externally;
Polymorphism: The methods of the parent class can be implemented in different ways after being inherited by the subclass;
Then you can talk about the advantages and disadvantages of object-oriented, then there are no absolute advantages and disadvantages for one thing, a comparison is needed. Therefore, you have to talk about Process-oriented programming ideas.
I’m back and lost my article
I saw that lz’s question was downvoted. I checked it in the community and found that there are indeed relevant answers. My article is a small supplement. If there are any questions, please point them out.
Object-oriented thinking
To add to my answer with an example from my work, I made a very simple function some time ago, Like
. It is indeed a very simple function, but the like function is not just for one time. Users can leave other comments. Like, you can like articles, you can like questions, etc., and likes may trigger a series of events (notifying relevant users, etc.). Initially, I thought of three ways to write it
<code>// 1.文章被用户点赞 $article->votedBy($user); // 2.用户给某篇文章点赞 $user->voteArticle($article); // 3.用户伸出大拇指给某篇文章点赞 $user->thumbUp()->voteToArticle($article); //$user->thumbUp()->voteToDiscuss($discuss);</code>
In the end, I used the third way of writing. You will know how to implement it the moment you stretch out your thumb.
This is really an interesting question, haha. Of course, everyone will have a different understanding. After all, the concept of object-oriented is also hyped.
My understanding includes two aspects:
One is object-oriented design;
One is object-oriented programming.
The core idea of object-oriented design is modularity, while the core idea of object-oriented programming is interface-oriented. Object-oriented programming also has a relative called object-based programming or prototype programming (JavaScript is one). Therefore, object-oriented programming generally requires language-level support, such as encapsulation, inheritance, and polymorphism as you mentioned above.
When the object-oriented concept was relatively popular, people often talked about software reuse, and inheritance was a direct manifestation of software reuse. But later people discovered that composition should be used more than inheritance, and then interface-oriented programming became popular.
I’ve gone a bit far, so let’s stop here.
Everything is an object.
object-oriented
everything is object
Be sure to use examples
For example, animals-》furu animals-》people, lions, tigers
Add polymorphism, for example, it will all be called
Inheritance, for example, furu animals are all viviparous, etc.
When implementing a project, you need to abstract the business into code. This abstraction process is the programming paradigm, and object-oriented is one of them. Similar ones include process-oriented and functional programming.
The characteristics of object-oriented are inheritance
, polymorphism
, encapsulation
.
Then the interviewer can start asking about inheritance
, polymorphism
, encapsulation
.
Perfect! ~~~
It would be better if you know what process-oriented and functional programming is.
self-contained, if he doesn’t understand, you can just leave. This interviewer is too good!
js said: Everything is an object, and objects are also objects--nothing wrong
Well, everything is an object
I remember reading an answer on zhihu. His understanding was that (in practice) the most important thing is code reuse.
Encapsulation: You can directly use the encapsulated code blocks, which is reuse.
Inheritance: Use existing attributes, which is reuse.
Polymorphism: Different details, but basically still need to be reused first.
Without discussing advanced programming, paradigms and other theoretical things, the most direct advantage and experience of writing code itself is that the code becomes more concise due to more reuse.
Facing your partner (commonly known as boyfriend/girlfriend), assume an elegant posture, and then tell him/her affectionately, look at me... This is to face your partner gracefully
Packaging data and associated behaviors
Interesting question. I am also a student and would like to share some of my own understanding.
A great feature of object-oriented is that it combines data and behavior (operations on data) at the same time, and is more similar to the operating logic of the real world.
Why I think so is based on comparison with traditional methods (process-oriented, etc.).
For example, the C language's processing of data and behavior is actually a bit separated, and it pays more attention to behavior. There is no concept called "object" in it, but a world full of actions (functions). Mainly, functions call functions, and results are given from beginning to end... The most important thing in this world is various actions.
The "object" of C++ is particularly like a thing, such as a person. Its world is a world full of objects. Mainly the relationships between objects (people and objects, people and people, etc.), these relationships or collisions lead to the final result... The most important thing in this world is various objects.
The objects themselves carry data and functions; only the relationship between objects makes the world run. At these two points, object-oriented programming is more similar to the real world.
The above-mentioned "inheritance, encapsulation, polymorphism" and other characteristics can also be used to simulate the real world to better understand it. Let me just give you some examples...
Inheritance: books & textbooks & computer textbooks - this is the inheritance relationship in the real world.
Packaging: Mobile phone - it is packaged, and when you use it, you don’t have to know the circuit logic inside.
Polymorphism: People.Look (Beauty) & People.Look (Strong Light) - The parameter types are different, and the execution is also different.
The benefits of object-oriented are also easy to understand. One is to improve understandability, because objects are presented according to the rules of human understanding of the world and conform to common ways of thinking; the second is to make it easier to develop and maintain. The optimization of the system is to a large extent equal to a new understanding of object data and functions. Modify some things based on the original.
What I said is wrong, please criticize and correct me...!
It cannot be ruled out that some interviewers are very stupid themselves. This is such a huge fucking topic that we can’t finish it even if we sit together for several days
Scroll less on Zhihu and write more code
Everything is an object.